home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Utilities / vim-5.1 / src / search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  82.6 KB  |  3,514 lines

  1. /* vi:set ts=8 sts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved    by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  */
  8. /*
  9.  * search.c: code for normal mode searching commands
  10.  */
  11.  
  12. #include "vim.h"
  13.  
  14. static int inmacro __ARGS((char_u *, char_u *));
  15. static int check_linecomment __ARGS((char_u *line));
  16. static int cls __ARGS((void));
  17. static int skip_chars __ARGS((int, int));
  18. #ifdef TEXT_OBJECTS
  19. static void back_in_line __ARGS((void));
  20. static void find_first_blank __ARGS((FPOS *));
  21. static void findsent_forward __ARGS((long count, int at_start_sent));
  22. #endif
  23. #ifdef FIND_IN_PATH
  24. static void show_pat_in_path __ARGS((char_u *, int,
  25.                      int, int, FILE *, linenr_t *, long));
  26. #endif
  27. #ifdef VIMINFO
  28. static void wvsp_one __ARGS((FILE *fp, int idx, char *s, char *sc));
  29. #endif
  30.  
  31. static char_u *top_bot_msg = (char_u *)"search hit TOP, continuing at BOTTOM";
  32. static char_u *bot_top_msg = (char_u *)"search hit BOTTOM, continuing at TOP";
  33.  
  34. /*
  35.  * This file contains various searching-related routines. These fall into
  36.  * three groups:
  37.  * 1. string searches (for /, ?, n, and N)
  38.  * 2. character searches within a single line (for f, F, t, T, etc)
  39.  * 3. "other" kinds of searches like the '%' command, and 'word' searches.
  40.  */
  41.  
  42. /*
  43.  * String searches
  44.  *
  45.  * The string search functions are divided into two levels:
  46.  * lowest:  searchit(); uses an FPOS for starting position and found match.
  47.  * Highest: do_search(); uses curwin->w_cursor; calls searchit().
  48.  *
  49.  * The last search pattern is remembered for repeating the same search.
  50.  * This pattern is shared between the :g, :s, ? and / commands.
  51.  * This is in search_regcomp().
  52.  *
  53.  * The actual string matching is done using a heavily modified version of
  54.  * Henry Spencer's regular expression library.  See regexp.c.
  55.  */
  56.  
  57. /* The offset for a search command is store in a soff struct */
  58. /* Note: only spats[0].off is really used */
  59. struct soffset
  60. {
  61.     int        dir;        /* search direction */
  62.     int        line;        /* search has line offset */
  63.     int        end;        /* search set cursor at end */
  64.     long    off;        /* line or char offset */
  65. };
  66.  
  67. /* A search pattern and its attributes are stored in a spat struct */
  68. struct spat
  69. {
  70.     char_u        *pat;    /* the pattern (in allocated memory) or NULL */
  71.     int            magic;    /* magicness of the pattern */
  72.     int            no_scs;    /* no smarcase for this pattern */
  73.     struct soffset  off;
  74. };
  75.  
  76. /*
  77.  * Two search patterns are remembered: One for the :substitute command and
  78.  * one for other searches.  last_idx points to the one that was used the last
  79.  * time.
  80.  */
  81. static struct spat spats[2] =
  82. {
  83.     {NULL, TRUE, FALSE, {'/', 0, 0, 0L}},    /* last used search pat */
  84.     {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}    /* last used substitute pat */
  85. };
  86.  
  87. static int last_idx = 0;    /* index in spats[] for RE_LAST */
  88.  
  89. #if defined(AUTOCMD) || defined(PROTO)
  90. /* copy of spats[], for keeping the search patterns while executing autocmds */
  91. static struct spat  saved_spats[2];
  92. static int        saved_last_idx = 0;
  93. #endif
  94.  
  95. static char_u        *mr_pattern = NULL;    /* pattern used by search_regcomp() */
  96.  
  97. #ifdef FIND_IN_PATH
  98. /*
  99.  * Type used by find_pattern_in_path() to remember which included files have
  100.  * been searched already.
  101.  */
  102. typedef struct SearchedFile
  103. {
  104.     FILE    *fp;        /* File pointer */
  105.     char_u    *name;        /* Full name of file */
  106.     linenr_t    lnum;        /* Line we were up to in file */
  107.     int        matched;    /* Found a match in this file */
  108. } SearchedFile;
  109. #endif
  110.  
  111. /*
  112.  * translate search pattern for vim_regcomp()
  113.  *
  114.  * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
  115.  * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
  116.  * pat_save == RE_BOTH: save pat in both patterns (:global command)
  117.  * pat_use  == RE_SEARCH: use previous search pattern if "pat" is NULL
  118.  * pat_use  == RE_SUBST: use previous sustitute pattern if "pat" is NULL
  119.  * pat_use  == RE_LAST: use last used pattern if "pat" is NULL
  120.  * options & SEARCH_HIS: put search string in history
  121.  * options & SEARCH_KEEP: keep previous search pattern
  122.  *
  123.  */
  124.     vim_regexp *
  125. search_regcomp(pat, pat_save, pat_use, options)
  126.     char_u  *pat;
  127.     int        pat_save;
  128.     int        pat_use;
  129.     int        options;
  130. {
  131.     int        magic;
  132.     int        i;
  133.  
  134.     rc_did_emsg = FALSE;
  135.     magic = p_magic;
  136.  
  137.     /*
  138.      * If no pattern given, use a previously defined pattern.
  139.      */
  140.     if (pat == NULL || *pat == NUL)
  141.     {
  142.     if (pat_use == RE_LAST)
  143.         i = last_idx;
  144.     else
  145.         i = pat_use;
  146.     if (spats[i].pat == NULL)    /* pattern was never defined */
  147.     {
  148.         if (pat_use == RE_SUBST)
  149.         emsg(e_nopresub);
  150.         else
  151.         emsg(e_noprevre);
  152.         rc_did_emsg = TRUE;
  153.         return (vim_regexp *)NULL;
  154.     }
  155.     pat = spats[i].pat;
  156.     magic = spats[i].magic;
  157.     no_smartcase = spats[i].no_scs;
  158.     }
  159.     else if (options & SEARCH_HIS)    /* put new pattern in history */
  160.     add_to_history(HIST_SEARCH, pat);
  161.  
  162.     mr_pattern = pat;
  163.  
  164.     /*
  165.      * Save the currently used pattern in the appropriate place,
  166.      * unless the pattern should not be remembered.
  167.      */
  168.     if (!(options & SEARCH_KEEP))
  169.     {
  170.     /*
  171.      * search or global command
  172.      */
  173.     if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
  174.     {
  175.         if (spats[RE_SEARCH].pat != pat)
  176.         {
  177.         vim_free(spats[RE_SEARCH].pat);
  178.         spats[RE_SEARCH].pat = vim_strsave(pat);
  179.         spats[RE_SEARCH].magic = magic;
  180.         spats[RE_SEARCH].no_scs = no_smartcase;
  181.         last_idx = RE_SEARCH;
  182.         /* If 'hlsearch' set and search pat changed: need redraw. */
  183. #ifdef EXTRA_SEARCH
  184.         if (p_hls)
  185.             redraw_all_later(NOT_VALID);
  186. #endif
  187.         }
  188.     }
  189.     /*
  190.      * substitute or global command
  191.      */
  192.     if (pat_save == RE_SUBST || pat_save == RE_BOTH)
  193.     {
  194.         if (spats[RE_SUBST].pat != pat)
  195.         {
  196.         vim_free(spats[RE_SUBST].pat);
  197.         spats[RE_SUBST].pat = vim_strsave(pat);
  198.         spats[RE_SUBST].magic = magic;
  199.         spats[RE_SUBST].no_scs = no_smartcase;
  200.         last_idx = RE_SUBST;
  201.         /* If 'hlsearch' set and search pat changed: need redraw. */
  202. #ifdef EXTRA_SEARCH
  203.         if (p_hls)
  204.             redraw_all_later(NOT_VALID);
  205. #endif
  206.         }
  207.     }
  208.     }
  209.  
  210.     set_reg_ic(pat);        /* tell the vim_regexec routine how to search */
  211.     return vim_regcomp(pat, magic);
  212. }
  213.  
  214. #if defined(AUTOCMD) || defined(PROTO)
  215. /*
  216.  * Save the search patterns, so they can be restored later.
  217.  * Used before/after executing autocommands.
  218.  */
  219.     void
  220. save_search_patterns()
  221. {
  222.     saved_spats[0] = spats[0];
  223.     if (spats[0].pat != NULL)
  224.     saved_spats[0].pat = vim_strsave(spats[0].pat);
  225.     saved_spats[1] = spats[1];
  226.     if (spats[1].pat != NULL)
  227.     saved_spats[1].pat = vim_strsave(spats[1].pat);
  228.     saved_last_idx = last_idx;
  229. }
  230.  
  231.     void
  232. restore_search_patterns()
  233. {
  234.     vim_free(spats[0].pat);
  235.     spats[0] = saved_spats[0];
  236.     vim_free(spats[1].pat);
  237.     spats[1] = saved_spats[1];
  238.     last_idx = saved_last_idx;
  239. }
  240. #endif
  241.  
  242. /*
  243.  * Set reg_ic according to p_ic, p_scs and the search pattern.
  244.  */
  245.     void
  246. set_reg_ic(pat)
  247.     char_u  *pat;
  248. {
  249.     char_u *p;
  250.  
  251.     reg_ic = p_ic;
  252.     if (reg_ic && !no_smartcase && p_scs
  253. #ifdef INSERT_EXPAND
  254.                 && !(ctrl_x_mode && curbuf->b_p_inf)
  255. #endif
  256.                                     )
  257.     {
  258.     /* don't ignore case if pattern has uppercase */
  259.     for (p = pat; *p; )
  260.         if (isupper(*p++))
  261.         reg_ic = FALSE;
  262.     }
  263.     no_smartcase = FALSE;
  264. }
  265.  
  266. #ifdef EXTRA_SEARCH
  267. /*
  268.  * Get a regexp program for the last used search pattern.
  269.  * This is used for highlighting all matches in a window.
  270.  */
  271.     vim_regexp *
  272. last_pat_prog()
  273. {
  274.     vim_regexp *prog;
  275.  
  276.     if (spats[last_idx].pat == NULL)
  277.     return NULL;
  278.     emsg_off = TRUE;    /* So it doesn't beep if bad expr */
  279.     prog = search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP);
  280.     emsg_off = FALSE;
  281.     return prog;
  282. }
  283. #endif
  284.  
  285. /*
  286.  * lowest level search function.
  287.  * Search for 'count'th occurrence of 'str' in direction 'dir'.
  288.  * Start at position 'pos' and return the found position in 'pos'.
  289.  *
  290.  * if (options & SEARCH_MSG) == 0 don't give any messages
  291.  * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
  292.  * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
  293.  * if (options & SEARCH_HIS) put search pattern in history
  294.  * if (options & SEARCH_END) return position at end of match
  295.  * if (options & SEARCH_START) accept match at pos itself
  296.  * if (options & SEARCH_KEEP) keep previous search pattern
  297.  *
  298.  * Return OK for success, FAIL for failure.
  299.  */
  300.     int
  301. searchit(buf, pos, dir, str, count, options, pat_use)
  302.     BUF        *buf;
  303.     FPOS    *pos;
  304.     int        dir;
  305.     char_u  *str;
  306.     long    count;
  307.     int        options;
  308.     int        pat_use;
  309. {
  310.     int            found;
  311.     linenr_t        lnum;        /* no init to shut up Apollo cc */
  312.     vim_regexp        *prog;
  313.     char_u        *ptr;
  314.     char_u        *match = NULL, *matchend = NULL;    /* init for GCC */
  315.     int            loop;
  316.     FPOS        start_pos;
  317.     int            at_first_line;
  318.     int            extra_col;
  319.     int            match_ok;
  320.     char_u        *p;
  321.  
  322.     if ((prog = search_regcomp(str, RE_SEARCH, pat_use,
  323.                  (options & (SEARCH_HIS + SEARCH_KEEP)))) == NULL)
  324.     {
  325.     if ((options & SEARCH_MSG) && !rc_did_emsg)
  326.         emsg2((char_u *)"Invalid search string: %s", mr_pattern);
  327.     return FAIL;
  328.     }
  329.  
  330.     if (options & SEARCH_START)
  331.     extra_col = 0;
  332.     else
  333.     extra_col = 1;
  334.  
  335.  
  336. /*
  337.  * find the string
  338.  */
  339.     do    /* loop for count */
  340.     {
  341.     start_pos = *pos;    /* remember start pos for detecting no match */
  342.     found = 0;        /* default: not found */
  343.     at_first_line = TRUE;    /* default: start in first line */
  344.     if (pos->lnum == 0)    /* correct lnum for when starting in line 0 */
  345.     {
  346.         pos->lnum = 1;
  347.         pos->col = 0;
  348.         at_first_line = FALSE;  /* not in first line now */
  349.     }
  350.  
  351.     /*
  352.      * Start searching in current line, unless searching backwards and
  353.      * we're in column 0.
  354.      */
  355.     if (dir == BACKWARD && start_pos.col == 0)
  356.     {
  357.         lnum = pos->lnum - 1;
  358.         at_first_line = FALSE;
  359.     }
  360.     else
  361.         lnum = pos->lnum;
  362.  
  363.     for (loop = 0; loop <= 1; ++loop)   /* loop twice if 'wrapscan' set */
  364.     {
  365.         for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
  366.                        lnum += dir, at_first_line = FALSE)
  367.         {
  368.         /*
  369.          * Look for a match somewhere in the line.
  370.          */
  371.         ptr = ml_get_buf(buf, lnum, FALSE);
  372.         if (vim_regexec(prog, ptr, TRUE))
  373.         {
  374.             match = prog->startp[0];
  375.             matchend = prog->endp[0];
  376.  
  377.             /*
  378.              * Forward search in the first line: match should be after
  379.              * the start position. If not, continue at the end of the
  380.              * match (this is vi compatible).
  381.              */
  382.             if (dir == FORWARD && at_first_line)
  383.             {
  384.             match_ok = TRUE;
  385.             /*
  386.              * When *match == NUL the cursor will be put one back
  387.              * afterwards, compare with that position, otherwise
  388.              * "/$" will get stuck on end of line.
  389.              */
  390.             while ((options & SEARCH_END) ?
  391.                          ((int)(matchend - ptr) - 1  <
  392.                          (int)start_pos.col + extra_col) :
  393.                   ((int)(match - ptr) - (int)(*match == NUL) <
  394.                           (int)start_pos.col + extra_col))
  395.             {
  396.                 /*
  397.                  * If vi-compatible searching, continue at the end
  398.                  * of the match, otherwise continue one position
  399.                  * forward.
  400.                  */
  401.                 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
  402.                 {
  403.                 p = matchend;
  404.                 if (match == p && *p != NUL)
  405.                     ++p;
  406.                 }
  407.                 else
  408.                 {
  409.                 p = match;
  410.                 if (*p != NUL)
  411.                     ++p;
  412.                 }
  413.                 if (*p != NUL && vim_regexec(prog, p, FALSE))
  414.                 {
  415.                 match = prog->startp[0];
  416.                 matchend = prog->endp[0];
  417.                 }
  418.                 else
  419.                 {
  420.                 match_ok = FALSE;
  421.                 break;
  422.                 }
  423.             }
  424.             if (!match_ok)
  425.                 continue;
  426.             }
  427.             if (dir == BACKWARD)
  428.             {
  429.             /*
  430.              * Now, if there are multiple matches on this line,
  431.              * we have to get the last one. Or the last one before
  432.              * the cursor, if we're on that line.
  433.              * When putting the new cursor at the end, compare
  434.              * relative to the end of the match.
  435.              */
  436.             match_ok = FALSE;
  437.             for (;;)
  438.             {
  439.                 if (!at_first_line || ((options & SEARCH_END) ?
  440.                     ((prog->endp[0] - ptr) - 1 + extra_col
  441.                               <= (int)start_pos.col) :
  442.                       ((prog->startp[0] - ptr) + extra_col
  443.                               <= (int)start_pos.col)))
  444.                 {
  445.                 match_ok = TRUE;
  446.                 match = prog->startp[0];
  447.                 matchend = prog->endp[0];
  448.                 }
  449.                 else
  450.                 break;
  451.                 /*
  452.                  * If vi-compatible searching, continue at the end
  453.                  * of the match, otherwise continue one position
  454.                  * forward.
  455.                  */
  456.                 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
  457.                 {
  458.                 p = matchend;
  459.                 if (p == match && *p != NUL)
  460.                     ++p;
  461.                 }
  462.                 else
  463.                 {
  464.                 p = match;
  465.                 if (*p != NUL)
  466.                     ++p;
  467.                 }
  468.                 if (*p == NUL || !vim_regexec(prog, p, (int)FALSE))
  469.                 break;
  470.             }
  471.  
  472.             /*
  473.              * If there is only a match after the cursor, skip
  474.              * this match.
  475.              */
  476.             if (!match_ok)
  477.                 continue;
  478.             }
  479.  
  480.             pos->lnum = lnum;
  481.             if (options & SEARCH_END && !(options & SEARCH_NOOF))
  482.             pos->col = (int) (matchend - ptr - 1);
  483.             else
  484.             pos->col = (int) (match - ptr);
  485.             found = 1;
  486.             break;
  487.         }
  488.         line_breakcheck();    /* stop if ctrl-C typed */
  489.         if (got_int)
  490.             break;
  491.  
  492.         if (loop && lnum == start_pos.lnum)
  493.             break;        /* if second loop, stop where started */
  494.         }
  495.         at_first_line = FALSE;
  496.  
  497.         /*
  498.          * stop the search if wrapscan isn't set, after an interrupt and
  499.          * after a match
  500.          */
  501.         if (!p_ws || got_int || found)
  502.         break;
  503.  
  504.         /*
  505.          * If 'wrapscan' is set we continue at the other end of the file.
  506.          * If 'shortmess' does not contain 's', we give a message.
  507.          * This message is also remembered in keep_msg for when the screen
  508.          * is redrawn. The keep_msg is cleared whenever another message is
  509.          * written.
  510.          */
  511.         if (dir == BACKWARD)    /* start second loop at the other end */
  512.         {
  513.         lnum = buf->b_ml.ml_line_count;
  514.         if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
  515.             give_warning(top_bot_msg, TRUE);
  516.         }
  517.         else
  518.         {
  519.         lnum = 1;
  520.         if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
  521.             give_warning(bot_top_msg, TRUE);
  522.         }
  523.     }
  524.     if (got_int)
  525.         break;
  526.     }
  527.     while (--count > 0 && found);   /* stop after count matches or no match */
  528.  
  529.     vim_free(prog);
  530.  
  531.     if (!found)            /* did not find it */
  532.     {
  533.     if (got_int)
  534.         emsg(e_interr);
  535.     else if ((options & SEARCH_MSG) == SEARCH_MSG)
  536.     {
  537.         if (p_ws)
  538.         emsg2(e_patnotf2, mr_pattern);
  539.         else if (lnum == 0)
  540.         EMSG2("search hit TOP without match for: %s", mr_pattern);
  541.         else
  542.         EMSG2("search hit BOTTOM without match for: %s", mr_pattern);
  543.     }
  544.     return FAIL;
  545.     }
  546.     search_match_len = matchend - match;
  547.  
  548.     return OK;
  549. }
  550.  
  551. /*
  552.  * Highest level string search function.
  553.  * Search for the 'count'th occurence of string 'str' in direction 'dirc'
  554.  *          If 'dirc' is 0: use previous dir.
  555.  *    If 'str' is NULL or empty : use previous string.
  556.  *    If 'options & SEARCH_REV' : go in reverse of previous dir.
  557.  *    If 'options & SEARCH_ECHO': echo the search command and handle options
  558.  *    If 'options & SEARCH_MSG' : may give error message
  559.  *    If 'options & SEARCH_OPT' : interpret optional flags
  560.  *    If 'options & SEARCH_HIS' : put search pattern in history
  561.  *    If 'options & SEARCH_NOOF': don't add offset to position
  562.  *    If 'options & SEARCH_MARK': set previous context mark
  563.  *    If 'options & SEARCH_KEEP': keep previous search pattern
  564.  *    If 'options & SEARCH_START': accept match at curpos itself
  565.  *
  566.  * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
  567.  * makes the movement linewise without moving the match position.
  568.  *
  569.  * return 0 for failure, 1 for found, 2 for found and line offset added
  570.  */
  571.     int
  572. do_search(oap, dirc, str, count, options)
  573.     OPARG        *oap;
  574.     int            dirc;
  575.     char_u       *str;
  576.     long        count;
  577.     int            options;
  578. {
  579.     FPOS        pos;    /* position of the last match */
  580.     char_u        *searchstr;
  581.     struct soffset  old_off;
  582.     int            retval;    /* Return value */
  583.     char_u        *p;
  584.     long        c;
  585.     char_u        *dircp;
  586.  
  587.     /*
  588.      * A line offset is not remembered, this is vi compatible.
  589.      */
  590.     if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
  591.     {
  592.     spats[0].off.line = FALSE;
  593.     spats[0].off.off = 0;
  594.     }
  595.  
  596.     /*
  597.      * Save the values for when (options & SEARCH_KEEP) is used.
  598.      * (there is no "if ()" around this because gcc wants them initialized)
  599.      */
  600.     old_off = spats[0].off;
  601.  
  602.     pos = curwin->w_cursor;    /* start searching at the cursor position */
  603.  
  604.     /*
  605.      * Find out the direction of the search.
  606.      */
  607.     if (dirc == 0)
  608.     dirc = spats[0].off.dir;
  609.     else
  610.     spats[0].off.dir = dirc;
  611.     if (options & SEARCH_REV)
  612.     {
  613. #ifdef WIN32
  614.     /* There is a bug in the Visual C++ 2.2 compiler which means that
  615.      * dirc always ends up being '/' */
  616.     dirc = (dirc == '/')  ?  '?'  :  '/';
  617. #else
  618.     if (dirc == '/')
  619.         dirc = '?';
  620.     else
  621.         dirc = '/';
  622. #endif
  623.     }
  624.  
  625.     /*
  626.      * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
  627.      */
  628.     for (;;)
  629.     {
  630.     searchstr = str;
  631.     dircp = NULL;
  632.                         /* use previous pattern */
  633.     if (str == NULL || *str == NUL || *str == dirc)
  634.     {
  635.         if (spats[RE_SEARCH].pat == NULL)        /* no previous pattern */
  636.         {
  637.         emsg(e_noprevre);
  638.         retval = 0;
  639.         goto end_do_search;
  640.         }
  641.         /* make search_regcomp() use spats[RE_SEARCH].pat */
  642.         searchstr = (char_u *)"";
  643.     }
  644.  
  645.     if (str != NULL && *str != NUL)    /* look for (new) offset */
  646.     {
  647.         /*
  648.          * Find end of regular expression.
  649.          * If there is a matching '/' or '?', toss it.
  650.          */
  651.         p = skip_regexp(str, dirc, (int)p_magic);
  652.         if (*p == dirc)
  653.         {
  654.         dircp = p;    /* remember where we put the NUL */
  655.         *p++ = NUL;
  656.         }
  657.         spats[0].off.line = FALSE;
  658.         spats[0].off.end = FALSE;
  659.         spats[0].off.off = 0;
  660.         /*
  661.          * Check for a line offset or a character offset.
  662.          * For get_address (echo off) we don't check for a character
  663.          * offset, because it is meaningless and the 's' could be a
  664.          * substitute command.
  665.          */
  666.         if (*p == '+' || *p == '-' || isdigit(*p))
  667.         spats[0].off.line = TRUE;
  668.         else if ((options & SEARCH_OPT) &&
  669.                     (*p == 'e' || *p == 's' || *p == 'b'))
  670.         {
  671.         if (*p == 'e')        /* end */
  672.             spats[0].off.end = SEARCH_END;
  673.         ++p;
  674.         }
  675.         if (isdigit(*p) || *p == '+' || *p == '-')       /* got an offset */
  676.         {
  677.                         /* 'nr' or '+nr' or '-nr' */
  678.         if (isdigit(*p) || isdigit(*(p + 1)))
  679.             spats[0].off.off = atol((char *)p);
  680.         else if (*p == '-')        /* single '-' */
  681.             spats[0].off.off = -1;
  682.         else                /* single '+' */
  683.             spats[0].off.off = 1;
  684.         ++p;
  685.         while (isdigit(*p))        /* skip number */
  686.             ++p;
  687.         }
  688.         searchcmdlen = p - str;        /* compute length of search command
  689.                                 for get_address() */
  690.         str = p;                /* put str after search command */
  691.     }
  692.  
  693.     if ((options & SEARCH_ECHO) && messaging())
  694.     {
  695.         msg_start();
  696.         msg_putchar(dirc);
  697.         msg_outtrans(*searchstr == NUL ? spats[RE_SEARCH].pat : searchstr);
  698.         if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
  699.         {
  700.         msg_putchar(dirc);
  701.         if (spats[0].off.end)
  702.             msg_putchar('e');
  703.         else if (!spats[0].off.line)
  704.             msg_putchar('s');
  705.         if (spats[0].off.off < 0)
  706.             msg_outnum((long)spats[0].off.off);
  707.         else if (spats[0].off.off > 0 || spats[0].off.line)
  708.         {
  709.             msg_putchar('+');
  710.             msg_outnum((long)spats[0].off.off);
  711.         }
  712.         }
  713.         msg_clr_eos();
  714.         (void)msg_check();
  715.  
  716.         gotocmdline(FALSE);
  717.         out_flush();
  718.         msg_nowait = TRUE;        /* don't wait for this message */
  719.     }
  720.  
  721.     /*
  722.      * If there is a character offset, subtract it from the current
  723.      * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
  724.      * This is not done for a line offset, because then we would not be vi
  725.      * compatible.
  726.      */
  727.     if (!spats[0].off.line && spats[0].off.off)
  728.     {
  729.         if (spats[0].off.off > 0)
  730.         {
  731.         for (c = spats[0].off.off; c; --c)
  732.             if (decl(&pos) == -1)
  733.             break;
  734.         if (c)            /* at start of buffer */
  735.         {
  736.             pos.lnum = 0;    /* allow lnum == 0 here */
  737.             pos.col = MAXCOL;
  738.         }
  739.         }
  740.         else
  741.         {
  742.         for (c = spats[0].off.off; c; ++c)
  743.             if (incl(&pos) == -1)
  744.             break;
  745.         if (c)            /* at end of buffer */
  746.         {
  747.             pos.lnum = curbuf->b_ml.ml_line_count + 1;
  748.             pos.col = 0;
  749.         }
  750.         }
  751.     }
  752.  
  753. #ifdef FKMAP        /* when in Farsi mode, reverse the character flow */
  754.     if (p_altkeymap && curwin->w_p_rl)
  755.          lrFswap(searchstr,0);
  756. #endif
  757.  
  758.     c = searchit(curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
  759.         searchstr, count, spats[0].off.end + (options &
  760.                (SEARCH_KEEP + SEARCH_HIS + SEARCH_MSG + SEARCH_START +
  761.                ((str != NULL && *str == ';') ? 0 : SEARCH_NOOF))),
  762.         2);
  763.     if (dircp != NULL)
  764.         *dircp = dirc;    /* restore second '/' or '?' for normal_cmd() */
  765.     if (c == FAIL)
  766.     {
  767.         retval = 0;
  768.         goto end_do_search;
  769.     }
  770.     if (spats[0].off.end && oap != NULL)
  771.         oap->inclusive = TRUE;  /* 'e' includes last character */
  772.  
  773.     retval = 1;            /* pattern found */
  774.  
  775.     /*
  776.      * Add character and/or line offset
  777.      */
  778.     if (!(options & SEARCH_NOOF) || *str == ';')
  779.     {
  780.         if (spats[0].off.line)    /* Add the offset to the line number. */
  781.         {
  782.         c = pos.lnum + spats[0].off.off;
  783.         if (c < 1)
  784.             pos.lnum = 1;
  785.         else if (c > curbuf->b_ml.ml_line_count)
  786.             pos.lnum = curbuf->b_ml.ml_line_count;
  787.         else
  788.             pos.lnum = c;
  789.         pos.col = 0;
  790.  
  791.         retval = 2;        /* pattern found, line offset added */
  792.         }
  793.         else
  794.         {
  795.         /* to the right, check for end of file */
  796.         if (spats[0].off.off > 0)
  797.         {
  798.             for (c = spats[0].off.off; c; --c)
  799.             if (incl(&pos) == -1)
  800.                 break;
  801.         }
  802.         /* to the left, check for start of file */
  803.         else
  804.         {
  805.             if ((c = pos.col + spats[0].off.off) >= 0)
  806.             pos.col = c;
  807.             else
  808.             for (c = spats[0].off.off; c; ++c)
  809.                 if (decl(&pos) == -1)
  810.                 break;
  811.         }
  812.         }
  813.     }
  814.  
  815.     /*
  816.      * The search command can be followed by a ';' to do another search.
  817.      * For example: "/pat/;/foo/+3;?bar"
  818.      * This is like doing another search command, except:
  819.      * - The remembered direction '/' or '?' is from the first search.
  820.      * - When an error happens the cursor isn't moved at all.
  821.      * Don't do this when called by get_address() (it handles ';' itself).
  822.      */
  823.     if (!(options & SEARCH_OPT) || str == NULL || *str != ';')
  824.         break;
  825.  
  826.     dirc = *++str;
  827.     if (dirc != '?' && dirc != '/')
  828.     {
  829.         retval = 0;
  830.         EMSG("Expected '?' or '/'  after ';'");
  831.         goto end_do_search;
  832.     }
  833.     ++str;
  834.     }
  835.  
  836.     if (options & SEARCH_MARK)
  837.     setpcmark();
  838.     curwin->w_cursor = pos;
  839.     curwin->w_set_curswant = TRUE;
  840.  
  841. end_do_search:
  842.     if (options & SEARCH_KEEP)
  843.     spats[0].off = old_off;
  844.     return retval;
  845. }
  846.  
  847. #if defined(INSERT_EXPAND) || defined(PROTO)
  848. /*
  849.  * search_for_exact_line(buf, pos, dir, pat)
  850.  *
  851.  * Search for a line starting with the given pattern (ignoring leading
  852.  * white-space), starting from pos and going in direction dir.    pos will
  853.  * contain the position of the match found.    Blank lines match only if
  854.  * ADDING is set.  if p_ic is set then the pattern must be in lowercase.
  855.  * Return OK for success, or FAIL if no line found.
  856.  */
  857.     int
  858. search_for_exact_line(buf, pos, dir, pat)
  859.     BUF        *buf;
  860.     FPOS    *pos;
  861.     int        dir;
  862.     char_u    *pat;
  863. {
  864.     linenr_t    start = 0;
  865.     char_u    *ptr;
  866.     char_u    *p;
  867.  
  868.     if (buf->b_ml.ml_line_count == 0)
  869.     return FAIL;
  870.     for (;;)
  871.     {
  872.     pos->lnum += dir;
  873.     if (pos->lnum < 1)
  874.     {
  875.         if (p_ws)
  876.         {
  877.         pos->lnum = buf->b_ml.ml_line_count;
  878.         if (!shortmess(SHM_SEARCH))
  879.             give_warning(top_bot_msg, TRUE);
  880.         }
  881.         else
  882.         {
  883.         pos->lnum = 1;
  884.         break;
  885.         }
  886.     }
  887.     else if (pos->lnum > buf->b_ml.ml_line_count)
  888.     {
  889.         if (p_ws)
  890.         {
  891.         pos->lnum = 1;
  892.         if (!shortmess(SHM_SEARCH))
  893.             give_warning(bot_top_msg, TRUE);
  894.         }
  895.         else
  896.         {
  897.         pos->lnum = 1;
  898.         break;
  899.         }
  900.     }
  901.     if (pos->lnum == start)
  902.         break;
  903.     if (start == 0)
  904.         start = pos->lnum;
  905.     ptr = ml_get_buf(buf, pos->lnum, FALSE);
  906.     p = skipwhite(ptr);
  907.     pos->col = p - ptr;
  908.  
  909.     /* when adding lines the matching line may be empty but it is not
  910.      * ignored because we are interested in the next line -- Acevedo */
  911.     if ((continue_status & CONT_ADDING) && !(continue_status & CONT_SOL))
  912.     {
  913.         if (p_ic)
  914.         {
  915.         /*
  916.         if (STRICMP(p, pat) == 0)
  917.         */
  918.         for (ptr = pat; TO_LOWER(*p) == *ptr && *p; p++, ptr++)
  919.             ;
  920.         if (*p == *ptr)    /* only possible if both NUL, exact match */
  921.             return OK;
  922.         }
  923.         else if (STRCMP(p, pat) == 0)
  924.         return OK;
  925.     }
  926.     else if (*p)    /* ignore empty lines */
  927.     {    /* expanding lines or words */
  928.         if (p_ic)
  929.         {
  930.         /*
  931.         if (STRNICMP(p, pat, completion_length) == 0)
  932.         */
  933.         for (ptr = pat; TO_LOWER(*p) == *ptr && *p; p++, ptr++)
  934.             ;
  935.         if (*ptr == NUL)
  936.             return OK;
  937.         }
  938.         else if (STRNCMP(p, pat, completion_length) == 0)
  939.         return OK;
  940.     }
  941.     }
  942.     return FAIL;
  943. }
  944. #endif /* INSERT_EXPAND */
  945.  
  946. /*
  947.  * Character Searches
  948.  */
  949.  
  950. /*
  951.  * searchc(c, dir, type, count)
  952.  *
  953.  * Search for character 'c', in direction 'dir'. If 'type' is 0, move to the
  954.  * position of the character, otherwise move to just before the char.
  955.  * Repeat this 'count' times.
  956.  */
  957.     int
  958. searchc(c, dir, type, count)
  959.     int            c;
  960.     int            dir;
  961.     int            type;
  962.     long        count;
  963. {
  964.     static int        lastc = NUL;    /* last character searched for */
  965.     static int        lastcdir;        /* last direction of character search */
  966.     static int        lastctype;        /* last type of search ("find" or "to") */
  967.     int            col;
  968.     char_u        *p;
  969.     int            len;
  970.  
  971.     if (c != NUL)    /* normal search: remember args for repeat */
  972.     {
  973.     if (!KeyStuffed)    /* don't remember when redoing */
  974.     {
  975.         lastc = c;
  976.         lastcdir = dir;
  977.         lastctype = type;
  978.     }
  979.     }
  980.     else        /* repeat previous search */
  981.     {
  982.     if (lastc == NUL)
  983.         return FALSE;
  984.     if (dir)    /* repeat in opposite direction */
  985.         dir = -lastcdir;
  986.     else
  987.         dir = lastcdir;
  988.     type = lastctype;
  989.     c = lastc;
  990.     }
  991.  
  992.     p = ml_get_curline();
  993.     col = curwin->w_cursor.col;
  994.     len = STRLEN(p);
  995.  
  996.     while (count--)
  997.     {
  998.     for (;;)
  999.     {
  1000.         if ((col += dir) < 0 || col >= len)
  1001.         return FALSE;
  1002.         if (p[col] == c)
  1003.         break;
  1004.     }
  1005.     }
  1006.     if (type)
  1007.     col -= dir;
  1008.     curwin->w_cursor.col = col;
  1009.     return TRUE;
  1010. }
  1011.  
  1012. /*
  1013.  * "Other" Searches
  1014.  */
  1015.  
  1016. /*
  1017.  * findmatch - find the matching paren or brace
  1018.  *
  1019.  * Improvement over vi: Braces inside quotes are ignored.
  1020.  */
  1021.     FPOS *
  1022. findmatch(oap, initc)
  1023.     OPARG   *oap;
  1024.     int        initc;
  1025. {
  1026.     return findmatchlimit(oap, initc, 0, 0);
  1027. }
  1028.  
  1029. /*
  1030.  * findmatchlimit -- find the matching paren or brace, if it exists within
  1031.  * maxtravel lines of here.  A maxtravel of 0 means search until you fall off
  1032.  * the edge of the file.
  1033.  *
  1034.  * flags: FM_BACKWARD    search backwards (when initc is '/', '*' or '#')
  1035.  *      FM_FORWARD    search forwards (when initc is '/', '*' or '#')
  1036.  *      FM_BLOCKSTOP    stop at start/end of block ({ or } in column 0)
  1037.  *      FM_SKIPCOMM    skip comments (not implemented yet!)
  1038.  */
  1039.  
  1040.     FPOS *
  1041. findmatchlimit(oap, initc, flags, maxtravel)
  1042.     OPARG   *oap;
  1043.     int        initc;
  1044.     int        flags;
  1045.     int        maxtravel;
  1046. {
  1047.     static FPOS        pos;        /* current search position */
  1048.     int            findc;        /* matching brace */
  1049.     int            c;
  1050.     int            count = 0;        /* cumulative number of braces */
  1051.     int            idx = 0;        /* init for gcc */
  1052.     static char_u   table[6] = {'(', ')', '[', ']', '{', '}'};
  1053. #ifdef RIGHTLEFT
  1054.     static char_u   table_rl[6] = {')', '(', ']', '[', '}', '{'};
  1055. #endif
  1056.     int            inquote = FALSE;    /* TRUE when inside quotes */
  1057.     char_u        *linep;        /* pointer to current line */
  1058.     char_u        *ptr;
  1059.     int            do_quotes;        /* check for quotes in current line */
  1060.     int            at_start;        /* do_quotes value at start position */
  1061.     int            hash_dir = 0;    /* Direction searched for # things */
  1062.     int            comment_dir = 0;    /* Direction searched for comments */
  1063.     FPOS        match_pos;        /* Where last slash-star was found */
  1064.     int            start_in_quotes;    /* start position is in quotes */
  1065.     int            traveled = 0;    /* how far we've searched so far */
  1066.     int            ignore_cend = FALSE;    /* ignore comment end */
  1067.     int            cpo_match;        /* vi compatible matching */
  1068.     int            dir;        /* Direction to search */
  1069.     int            comment_col = MAXCOL;   /* start of / / comment */
  1070.  
  1071.     pos = curwin->w_cursor;
  1072.     linep = ml_get(pos.lnum);
  1073.  
  1074.     cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
  1075.  
  1076.     /* Direction to search when initc is '/', '*' or '#' */
  1077.     if (flags & FM_BACKWARD)
  1078.     dir = BACKWARD;
  1079.     else if (flags & FM_FORWARD)
  1080.     dir = FORWARD;
  1081.     else
  1082.     dir = 0;
  1083.  
  1084.     /*
  1085.      * if initc given, look in the table for the matching character
  1086.      * '/' and '*' are special cases: look for start or end of comment.
  1087.      * When '/' is used, we ignore running backwards into an star-slash, for
  1088.      * "[*" command, we just want to find any comment.
  1089.      */
  1090.     if (initc == '/' || initc == '*')
  1091.     {
  1092.     comment_dir = dir;
  1093.     if (initc == '/')
  1094.         ignore_cend = TRUE;
  1095.     idx = (dir == FORWARD) ? 0 : 1;
  1096.     initc = NUL;
  1097.     }
  1098.     else if (initc != '#' && initc != NUL)
  1099.     {
  1100.     for (idx = 0; idx < 6; ++idx)
  1101. #ifdef RIGHTLEFT
  1102.         if ((curwin->w_p_rl ? table_rl : table)[idx] == initc)
  1103.         {
  1104.         initc = (curwin->w_p_rl ? table_rl : table)[idx = idx ^ 1];
  1105. #else
  1106.         if (table[idx] == initc)
  1107.         {
  1108.         initc = table[idx = idx ^ 1];        /* balancing } */
  1109. #endif
  1110.         break;
  1111.         }
  1112.     if (idx == 6)        /* invalid initc! */
  1113.         return NULL;
  1114.     }
  1115.     /*
  1116.      * Either initc is '#', or no initc was given and we need to look under the
  1117.      * cursor.
  1118.      */
  1119.     else
  1120.     {
  1121.     if (initc == '#')
  1122.     {
  1123.         hash_dir = dir;
  1124.     }
  1125.     else
  1126.     {
  1127.         /*
  1128.          * initc was not given, must look for something to match under
  1129.          * or near the cursor.
  1130.          * Only check for special things when 'cpo' doesn't have '%'.
  1131.          */
  1132.         if (!cpo_match)
  1133.         {
  1134.         /* Are we before or at #if, #else etc.? */
  1135.         ptr = skipwhite(linep);
  1136.         if (*ptr == '#' && pos.col <= (colnr_t)(ptr - linep))
  1137.         {
  1138.             ptr = skipwhite(ptr + 1);
  1139.             if (   STRNCMP(ptr, "if", 2) == 0
  1140.             || STRNCMP(ptr, "endif", 5) == 0
  1141.             || STRNCMP(ptr, "el", 2) == 0)
  1142.             hash_dir = 1;
  1143.         }
  1144.  
  1145.         /* Are we on a comment? */
  1146.         else if (linep[pos.col] == '/')
  1147.         {
  1148.             if (linep[pos.col + 1] == '*')
  1149.             {
  1150.             comment_dir = FORWARD;
  1151.             idx = 0;
  1152.             pos.col++;
  1153.             }
  1154.             else if (pos.col > 0 && linep[pos.col - 1] == '*')
  1155.             {
  1156.             comment_dir = BACKWARD;
  1157.             idx = 1;
  1158.             pos.col--;
  1159.             }
  1160.         }
  1161.         else if (linep[pos.col] == '*')
  1162.         {
  1163.             if (linep[pos.col + 1] == '/')
  1164.             {
  1165.             comment_dir = BACKWARD;
  1166.             idx = 1;
  1167.             }
  1168.             else if (pos.col > 0 && linep[pos.col - 1] == '/')
  1169.             {
  1170.             comment_dir = FORWARD;
  1171.             idx = 0;
  1172.             }
  1173.         }
  1174.         }
  1175.  
  1176.         /*
  1177.          * If we are not on a comment or the # at the start of a line, then
  1178.          * look for brace anywhere on this line after the cursor.
  1179.          */
  1180.         if (!hash_dir && !comment_dir)
  1181.         {
  1182.         /*
  1183.          * find the brace under or after the cursor
  1184.          */
  1185.         idx = 6;            /* error if this line is empty */
  1186.         for (;;)
  1187.         {
  1188.             initc = linep[pos.col];
  1189.             if (initc == NUL)
  1190.             break;
  1191.  
  1192.             for (idx = 0; idx < 6; ++idx)
  1193. #ifdef RIGHTLEFT
  1194.             if ((curwin->w_p_rl ? table_rl : table)[idx] == initc)
  1195. #else
  1196.             if (table[idx] == initc)
  1197. #endif
  1198.                 break;
  1199.             if (idx != 6)
  1200.             break;
  1201.             ++pos.col;
  1202.         }
  1203.         if (idx == 6)
  1204.         {
  1205.             /* no brace in the line, maybe use "  #if" then */
  1206.             if (!cpo_match && *skipwhite(linep) == '#')
  1207.             hash_dir = 1;
  1208.             else
  1209.             return NULL;
  1210.         }
  1211.         }
  1212.     }
  1213.     if (hash_dir)
  1214.     {
  1215.         /*
  1216.          * Look for matching #if, #else, #elif, or #endif
  1217.          */
  1218.         if (oap != NULL)
  1219.         oap->motion_type = MLINE;   /* Linewise for this case only */
  1220.         if (initc != '#')
  1221.         {
  1222.         ptr = skipwhite(skipwhite(linep) + 1);
  1223.         if (STRNCMP(ptr, "if", (size_t)2) == 0 ||
  1224.                        STRNCMP(ptr, "el", (size_t)2) == 0)
  1225.             hash_dir = 1;
  1226.         else if (STRNCMP(ptr, "endif", (size_t)5) == 0)
  1227.             hash_dir = -1;
  1228.         else
  1229.             return NULL;
  1230.         }
  1231.         pos.col = 0;
  1232.         while (!got_int)
  1233.         {
  1234.         if (hash_dir > 0)
  1235.         {
  1236.             if (pos.lnum == curbuf->b_ml.ml_line_count)
  1237.             break;
  1238.         }
  1239.         else if (pos.lnum == 1)
  1240.             break;
  1241.         pos.lnum += hash_dir;
  1242.         linep = ml_get(pos.lnum);
  1243.         line_breakcheck();    /* check for CTRL-C typed */
  1244.         ptr = skipwhite(linep);
  1245.         if (*ptr != '#')
  1246.             continue;
  1247.         pos.col = ptr - linep;
  1248.         ptr = skipwhite(ptr + 1);
  1249.         if (hash_dir > 0)
  1250.         {
  1251.             if (STRNCMP(ptr, "if", (size_t)2) == 0)
  1252.             count++;
  1253.             else if (STRNCMP(ptr, "el", (size_t)2) == 0)
  1254.             {
  1255.             if (count == 0)
  1256.                 return &pos;
  1257.             }
  1258.             else if (STRNCMP(ptr, "endif", (size_t)5) == 0)
  1259.             {
  1260.             if (count == 0)
  1261.                 return &pos;
  1262.             count--;
  1263.             }
  1264.         }
  1265.         else
  1266.         {
  1267.             if (STRNCMP(ptr, "if", (size_t)2) == 0)
  1268.             {
  1269.             if (count == 0)
  1270.                 return &pos;
  1271.             count--;
  1272.             }
  1273.             else if (initc == '#' && STRNCMP(ptr, "el", (size_t)2) == 0)
  1274.             {
  1275.             if (count == 0)
  1276.                 return &pos;
  1277.             }
  1278.             else if (STRNCMP(ptr, "endif", (size_t)5) == 0)
  1279.             count++;
  1280.         }
  1281.         }
  1282.         return NULL;
  1283.     }
  1284.     }
  1285.  
  1286. #ifdef RIGHTLEFT
  1287.     findc = (curwin->w_p_rl ? table_rl : table)[idx ^ 1];
  1288. #else
  1289.     findc = table[idx ^ 1];    /* get matching brace */
  1290. #endif
  1291.     idx &= 1;
  1292.  
  1293.     do_quotes = -1;
  1294.     start_in_quotes = MAYBE;
  1295.     /* backward search: Check if this line contains a single-line comment */
  1296.     if (idx && comment_dir)
  1297.     comment_col = check_linecomment(linep);
  1298.     while (!got_int)
  1299.     {
  1300.     /*
  1301.      * Go to the next position, forward or backward. We could use
  1302.      * inc() and dec() here, but that is much slower
  1303.      */
  1304.     if (idx)            /* backward search */
  1305.     {
  1306.         if (pos.col == 0)        /* at start of line, go to prev. one */
  1307.         {
  1308.         if (pos.lnum == 1)    /* start of file */
  1309.             break;
  1310.         --pos.lnum;
  1311.  
  1312.         if (maxtravel && traveled++ > maxtravel)
  1313.             break;
  1314.  
  1315.         linep = ml_get(pos.lnum);
  1316.         pos.col = STRLEN(linep);    /* put pos.col on trailing NUL */
  1317.         do_quotes = -1;
  1318.         line_breakcheck();
  1319.  
  1320.         /* Check if this line contains a single-line comment */
  1321.         if (comment_dir)
  1322.             comment_col = check_linecomment(linep);
  1323.         }
  1324.         else
  1325.         --pos.col;
  1326.     }
  1327.     else                /* forward search */
  1328.     {
  1329.         if (linep[pos.col] == NUL)    /* at end of line, go to next one */
  1330.         {
  1331.         if (pos.lnum == curbuf->b_ml.ml_line_count) /* end of file */
  1332.             break;
  1333.         ++pos.lnum;
  1334.  
  1335.         if (maxtravel && traveled++ > maxtravel)
  1336.             break;
  1337.  
  1338.         linep = ml_get(pos.lnum);
  1339.         pos.col = 0;
  1340.         do_quotes = -1;
  1341.         line_breakcheck();
  1342.         }
  1343.         else
  1344.         ++pos.col;
  1345.     }
  1346.  
  1347.     /*
  1348.      * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
  1349.      */
  1350.     if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
  1351.                      (linep[0] == '{' || linep[0] == '}'))
  1352.     {
  1353.         if (linep[0] == findc && count == 0)    /* match! */
  1354.         return &pos;
  1355.         break;                    /* out of scope */
  1356.     }
  1357.  
  1358.     if (comment_dir)
  1359.     {
  1360.         /* Note: comments do not nest, and we ignore quotes in them */
  1361.         /* TODO: ignore comment brackets inside strings */
  1362.         if (comment_dir == FORWARD)
  1363.         {
  1364.         if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
  1365.         {
  1366.             pos.col++;
  1367.             return &pos;
  1368.         }
  1369.         }
  1370.         else    /* Searching backwards */
  1371.         {
  1372.         /*
  1373.          * A comment may contain / * or / /, it may also start or end
  1374.          * with / * /.    Ignore a / * after / /.
  1375.          */
  1376.         if (pos.col == 0)
  1377.             continue;
  1378.         else if (  linep[pos.col - 1] == '/'
  1379.             && linep[pos.col] == '*'
  1380.             && (int)pos.col < comment_col)
  1381.         {
  1382.             count++;
  1383.             match_pos = pos;
  1384.             match_pos.col--;
  1385.         }
  1386.         else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
  1387.         {
  1388.             if (count > 0)
  1389.             pos = match_pos;
  1390.             else if (pos.col > 1 && linep[pos.col - 2] == '/')
  1391.             pos.col -= 2;
  1392.             else if (ignore_cend)
  1393.             continue;
  1394.             else
  1395.             return NULL;
  1396.             return &pos;
  1397.         }
  1398.         }
  1399.         continue;
  1400.     }
  1401.  
  1402.     /*
  1403.      * If smart matching ('cpoptions' does not contain '%'), braces inside
  1404.      * of quotes are ignored, but only if there is an even number of
  1405.      * quotes in the line.
  1406.      */
  1407.     if (cpo_match)
  1408.         do_quotes = 0;
  1409.     else if (do_quotes == -1)
  1410.     {
  1411.         /*
  1412.          * count the number of quotes in the line, skipping \" and '"'
  1413.          */
  1414.         at_start = do_quotes;
  1415.         for (ptr = linep; *ptr; ++ptr)
  1416.         {
  1417.         if (ptr == linep + curwin->w_cursor.col)
  1418.             at_start = (do_quotes & 1);
  1419.         if (*ptr == '"' && (ptr == linep || ptr[-1] != '\\') &&
  1420.                 (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
  1421.             ++do_quotes;
  1422.         }
  1423.         do_quotes &= 1;        /* result is 1 with even number of quotes */
  1424.  
  1425.         /*
  1426.          * If we find an uneven count, check current line and previous
  1427.          * one for a '\' at the end.
  1428.          */
  1429.         if (!do_quotes)
  1430.         {
  1431.         inquote = FALSE;
  1432.         if (ptr[-1] == '\\')
  1433.         {
  1434.             do_quotes = 1;
  1435.             if (start_in_quotes == MAYBE)
  1436.             {
  1437.             inquote = !at_start;
  1438.             if (inquote)
  1439.                 start_in_quotes = TRUE;
  1440.             }
  1441.             else if (idx)        /* backward search */
  1442.             inquote = TRUE;
  1443.         }
  1444.         if (pos.lnum > 1)
  1445.         {
  1446.             ptr = ml_get(pos.lnum - 1);
  1447.             if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
  1448.             {
  1449.             do_quotes = 1;
  1450.             if (start_in_quotes == MAYBE)
  1451.             {
  1452.                 inquote = at_start;
  1453.                 if (inquote)
  1454.                 start_in_quotes = TRUE;
  1455.             }
  1456.             else if (!idx)            /* forward search */
  1457.                 inquote = TRUE;
  1458.             }
  1459.         }
  1460.         }
  1461.     }
  1462.     if (start_in_quotes == MAYBE)
  1463.         start_in_quotes = FALSE;
  1464.  
  1465.     /*
  1466.      * If 'smartmatch' is set:
  1467.      *   Things inside quotes are ignored by setting 'inquote'.  If we
  1468.      *   find a quote without a preceding '\' invert 'inquote'.  At the
  1469.      *   end of a line not ending in '\' we reset 'inquote'.
  1470.      *
  1471.      *   In lines with an uneven number of quotes (without preceding '\')
  1472.      *   we do not know which part to ignore. Therefore we only set
  1473.      *   inquote if the number of quotes in a line is even, unless this
  1474.      *   line or the previous one ends in a '\'.  Complicated, isn't it?
  1475.      */
  1476.     switch (c = linep[pos.col])
  1477.     {
  1478.     case NUL:
  1479.         /* at end of line without trailing backslash, reset inquote */
  1480.         if (pos.col == 0 || linep[pos.col - 1] != '\\')
  1481.         {
  1482.         inquote = FALSE;
  1483.         start_in_quotes = FALSE;
  1484.         }
  1485.         break;
  1486.  
  1487.     case '"':
  1488.         /* a quote that is preceded with a backslash is ignored */
  1489.         if (do_quotes && (pos.col == 0 || linep[pos.col - 1] != '\\'))
  1490.         {
  1491.         inquote = !inquote;
  1492.         start_in_quotes = FALSE;
  1493.         }
  1494.         break;
  1495.  
  1496.     /*
  1497.      * If smart matching ('cpoptions' does not contain '%'):
  1498.      *   Skip things in single quotes: 'x' or '\x'.  Be careful for single
  1499.      *   single quotes, eg jon's.  Things like '\233' or '\x3f' are not
  1500.      *   skipped, there is never a brace in them.
  1501.      */
  1502.     case '\'':
  1503.         if (!cpo_match)
  1504.         {
  1505.         if (idx)            /* backward search */
  1506.         {
  1507.             if (pos.col > 1)
  1508.             {
  1509.             if (linep[pos.col - 2] == '\'')
  1510.                 pos.col -= 2;
  1511.             else if (linep[pos.col - 2] == '\\' &&
  1512.                     pos.col > 2 && linep[pos.col - 3] == '\'')
  1513.                 pos.col -= 3;
  1514.             }
  1515.         }
  1516.         else if (linep[pos.col + 1])    /* forward search */
  1517.         {
  1518.             if (linep[pos.col + 1] == '\\' &&
  1519.                 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
  1520.             pos.col += 3;
  1521.             else if (linep[pos.col + 2] == '\'')
  1522.             pos.col += 2;
  1523.         }
  1524.         }
  1525.         break;
  1526.  
  1527.     default:
  1528.             /* Check for match outside of quotes, and inside of
  1529.              * quotes when the start is also inside of quotes */
  1530.         if (!inquote || start_in_quotes == TRUE)
  1531.         {
  1532.         if (c == initc)
  1533.             count++;
  1534.         else if (c == findc)
  1535.         {
  1536.             if (count == 0)
  1537.             return &pos;
  1538.             count--;
  1539.         }
  1540.         }
  1541.     }
  1542.     }
  1543.  
  1544.     if (comment_dir == BACKWARD && count > 0)
  1545.     {
  1546.     pos = match_pos;
  1547.     return &pos;
  1548.     }
  1549.     return (FPOS *) NULL;    /* never found it */
  1550. }
  1551.  
  1552. /*
  1553.  * Check if line[] contains a / / comment.
  1554.  * Return MAXCOL if not, otherwise return the column.
  1555.  * TODO: skip strings.
  1556.  */
  1557.     static int
  1558. check_linecomment(line)
  1559.     char_u    *line;
  1560. {
  1561.     char_u  *p;
  1562.  
  1563.     p = line;
  1564.     while ((p = vim_strchr(p, '/')) != NULL)
  1565.     {
  1566.     if (p[1] == '/')
  1567.         break;
  1568.     ++p;
  1569.     }
  1570.  
  1571.     if (p == NULL)
  1572.     return MAXCOL;
  1573.     return (int)(p - line);
  1574. }
  1575.  
  1576. /*
  1577.  * Move cursor briefly to character matching the one under the cursor.
  1578.  * Show the match only if it is visible on the screen.
  1579.  */
  1580.     void
  1581. showmatch()
  1582. {
  1583.     FPOS       *lpos, save_cursor;
  1584.     FPOS        mpos;
  1585.     colnr_t        vcol;
  1586.     long        save_so;
  1587. #ifdef USE_GUI
  1588.     int            save_state;
  1589. #endif
  1590.  
  1591.     if ((lpos = findmatch(NULL, NUL)) == NULL)        /* no match, so beep */
  1592.     vim_beep();
  1593.     else if (lpos->lnum >= curwin->w_topline)
  1594.     {
  1595.     if (!curwin->w_p_wrap)
  1596.         getvcol(curwin, lpos, NULL, &vcol, NULL);
  1597.     if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol &&
  1598.                       vcol < curwin->w_leftcol + Columns))
  1599.     {
  1600.         mpos = *lpos;    /* save the pos, update_screen() may change it */
  1601.         update_screen(VALID_TO_CURSCHAR); /* show the new char first */
  1602.         save_cursor = curwin->w_cursor;
  1603.         save_so = p_so;
  1604. #ifdef USE_GUI
  1605.         save_state = State;
  1606.         State = SHOWMATCH;
  1607.         if (gui.in_use)
  1608.         gui_upd_cursor_shape();    /* change shape of cursor */
  1609. #endif
  1610.         curwin->w_cursor = mpos;    /* move to matching char */
  1611.         p_so = 0;            /* don't use 'scrolloff' here */
  1612.         showruler(FALSE);
  1613.         setcursor();
  1614.         cursor_on();        /* make sure that the cursor is shown */
  1615.         out_flush();
  1616.  
  1617.         /*
  1618.          * brief pause, unless 'm' is present in 'cpo' and a character is
  1619.          * available.
  1620.          */
  1621.         if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
  1622.         ui_delay(p_mat * 100L, TRUE);
  1623.         else if (!char_avail())
  1624.         ui_delay(p_mat * 100L, FALSE);
  1625.         curwin->w_cursor = save_cursor;    /* restore cursor position */
  1626.         p_so = save_so;
  1627. #ifdef USE_GUI
  1628.         State = save_state;
  1629.         if (gui.in_use)
  1630.         gui_upd_cursor_shape();    /* change shape of cursor */
  1631. #endif
  1632.     }
  1633.     }
  1634. }
  1635.  
  1636. /*
  1637.  * findsent(dir, count) - Find the start of the next sentence in direction
  1638.  * 'dir' Sentences are supposed to end in ".", "!" or "?" followed by white
  1639.  * space or a line break. Also stop at an empty line.
  1640.  * Return OK if the next sentence was found.
  1641.  */
  1642.     int
  1643. findsent(dir, count)
  1644.     int        dir;
  1645.     long    count;
  1646. {
  1647.     FPOS        pos, tpos;
  1648.     int            c;
  1649.     int            (*func) __ARGS((FPOS *));
  1650.     int            startlnum;
  1651.     int            noskip = FALSE;        /* do not skip blanks */
  1652.  
  1653.     pos = curwin->w_cursor;
  1654.     if (dir == FORWARD)
  1655.     func = incl;
  1656.     else
  1657.     func = decl;
  1658.  
  1659.     while (count--)
  1660.     {
  1661.     /*
  1662.      * if on an empty line, skip upto a non-empty line
  1663.      */
  1664.     if (gchar(&pos) == NUL)
  1665.     {
  1666.         do
  1667.         if ((*func)(&pos) == -1)
  1668.             break;
  1669.         while (gchar(&pos) == NUL);
  1670.         if (dir == FORWARD)
  1671.         goto found;
  1672.     }
  1673.     /*
  1674.      * if on the start of a paragraph or a section and searching forward,
  1675.      * go to the next line
  1676.      */
  1677.     else if (dir == FORWARD && pos.col == 0 &&
  1678.                         startPS(pos.lnum, NUL, FALSE))
  1679.     {
  1680.         if (pos.lnum == curbuf->b_ml.ml_line_count)
  1681.         return FAIL;
  1682.         ++pos.lnum;
  1683.         goto found;
  1684.     }
  1685.     else if (dir == BACKWARD)
  1686.         decl(&pos);
  1687.  
  1688.     /* go back to the previous non-blank char */
  1689.     while ((c = gchar(&pos)) == ' ' || c == '\t' ||
  1690.          (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
  1691.     {
  1692.         if (decl(&pos) == -1)
  1693.         break;
  1694.         /* when going forward: Stop in front of empty line */
  1695.         if (lineempty(pos.lnum) && dir == FORWARD)
  1696.         {
  1697.         incl(&pos);
  1698.         goto found;
  1699.         }
  1700.     }
  1701.  
  1702.     /* remember the line where the search started */
  1703.     startlnum = pos.lnum;
  1704.  
  1705.     for (;;)        /* find end of sentence */
  1706.     {
  1707.         c = gchar(&pos);
  1708.         if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
  1709.         {
  1710.         if (dir == BACKWARD && pos.lnum != startlnum)
  1711.             ++pos.lnum;
  1712.         break;
  1713.         }
  1714.         if (c == '.' || c == '!' || c == '?')
  1715.         {
  1716.         tpos = pos;
  1717.         do
  1718.             if ((c = inc(&tpos)) == -1)
  1719.             break;
  1720.         while (vim_strchr((char_u *)")]\"'", c = gchar(&tpos)) != NULL);
  1721.         if (c == -1  || c == ' ' || c == '\t' || c == NUL)
  1722.         {
  1723.             pos = tpos;
  1724.             if (gchar(&pos) == NUL) /* skip NUL at EOL */
  1725.             inc(&pos);
  1726.             break;
  1727.         }
  1728.         }
  1729.         if ((*func)(&pos) == -1)
  1730.         {
  1731.         if (count)
  1732.             return FAIL;
  1733.         noskip = TRUE;
  1734.         break;
  1735.         }
  1736.     }
  1737. found:
  1738.         /* skip white space */
  1739.     while (!noskip && ((c = gchar(&pos)) == ' ' || c == '\t'))
  1740.         if (incl(&pos) == -1)
  1741.         break;
  1742.     }
  1743.  
  1744.     setpcmark();
  1745.     curwin->w_cursor = pos;
  1746.     return OK;
  1747. }
  1748.  
  1749. /*
  1750.  * findpar(dir, count, what) - Find the next paragraph in direction 'dir'
  1751.  * Paragraphs are currently supposed to be separated by empty lines.
  1752.  * Return TRUE if the next paragraph was found.
  1753.  * If 'what' is '{' or '}' we go to the next section.
  1754.  * If 'both' is TRUE also stop at '}'.
  1755.  */
  1756.     int
  1757. findpar(oap, dir, count, what, both)
  1758.     OPARG        *oap;
  1759.     int            dir;
  1760.     long        count;
  1761.     int            what;
  1762.     int            both;
  1763. {
  1764.     linenr_t    curr;
  1765.     int        did_skip;   /* TRUE after separating lines have been skipped */
  1766.     int        first;        /* TRUE on first line */
  1767.  
  1768.     curr = curwin->w_cursor.lnum;
  1769.  
  1770.     while (count--)
  1771.     {
  1772.     did_skip = FALSE;
  1773.     for (first = TRUE; ; first = FALSE)
  1774.     {
  1775.         if (*ml_get(curr) != NUL)
  1776.         did_skip = TRUE;
  1777.  
  1778.         if (!first && did_skip && startPS(curr, what, both))
  1779.         break;
  1780.  
  1781.         if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
  1782.         {
  1783.         if (count)
  1784.             return FALSE;
  1785.         curr -= dir;
  1786.         break;
  1787.         }
  1788.     }
  1789.     }
  1790.     setpcmark();
  1791.     if (both && *ml_get(curr) == '}')    /* include line with '}' */
  1792.     ++curr;
  1793.     curwin->w_cursor.lnum = curr;
  1794.     if (curr == curbuf->b_ml.ml_line_count)
  1795.     {
  1796.     if ((curwin->w_cursor.col = STRLEN(ml_get(curr))) != 0)
  1797.     {
  1798.         --curwin->w_cursor.col;
  1799.         oap->inclusive = TRUE;
  1800.     }
  1801.     }
  1802.     else
  1803.     curwin->w_cursor.col = 0;
  1804.     return TRUE;
  1805. }
  1806.  
  1807. /*
  1808.  * check if the string 's' is a nroff macro that is in option 'opt'
  1809.  */
  1810.     static int
  1811. inmacro(opt, s)
  1812.     char_u    *opt;
  1813.     char_u    *s;
  1814. {
  1815.     char_u    *macro;
  1816.  
  1817.     for (macro = opt; macro[0]; ++macro)
  1818.     {
  1819.     if (macro[0] == s[0] && (((s[1] == NUL || s[1] == ' ') &&
  1820.            (macro[1] == NUL || macro[1] == ' ')) || macro[1] == s[1]))
  1821.         break;
  1822.     ++macro;
  1823.     if (macro[0] == NUL)
  1824.         break;
  1825.     }
  1826.     return (macro[0] != NUL);
  1827. }
  1828.  
  1829. /*
  1830.  * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
  1831.  * If 'para' is '{' or '}' only check for sections.
  1832.  * If 'both' is TRUE also stop at '}'
  1833.  */
  1834.     int
  1835. startPS(lnum, para, both)
  1836.     linenr_t    lnum;
  1837.     int        para;
  1838.     int        both;
  1839. {
  1840.     char_u    *s;
  1841.  
  1842.     s = ml_get(lnum);
  1843.     if (*s == para || *s == '\f' || (both && *s == '}'))
  1844.     return TRUE;
  1845.     if (*s == '.' && (inmacro(p_sections, s + 1) ||
  1846.                        (!para && inmacro(p_para, s + 1))))
  1847.     return TRUE;
  1848.     return FALSE;
  1849. }
  1850.  
  1851. /*
  1852.  * The following routines do the word searches performed by the 'w', 'W',
  1853.  * 'b', 'B', 'e', and 'E' commands.
  1854.  */
  1855.  
  1856. /*
  1857.  * To perform these searches, characters are placed into one of three
  1858.  * classes, and transitions between classes determine word boundaries.
  1859.  *
  1860.  * The classes are:
  1861.  *
  1862.  * 0 - white space
  1863.  * 1 - keyword charactes (letters, digits and underscore)
  1864.  * 2 - everything else
  1865.  */
  1866.  
  1867. static int    stype;        /* type of the word motion being performed */
  1868.  
  1869. /*
  1870.  * cls() - returns the class of character at curwin->w_cursor
  1871.  *
  1872.  * The 'type' of the current search modifies the classes of characters if a
  1873.  * 'W', 'B', or 'E' motion is being done. In this case, chars. from class 2
  1874.  * are reported as class 1 since only white space boundaries are of interest.
  1875.  */
  1876.     static int
  1877. cls()
  1878. {
  1879.     int        c;
  1880.  
  1881.     c = gchar_cursor();
  1882. #ifdef FKMAP    /* when 'akm' (Farsi mode), take care of Farsi blank */
  1883.     if (p_altkeymap && c == F_BLANK)
  1884.     return 0;
  1885. #endif
  1886.     if (c == ' ' || c == '\t' || c == NUL)
  1887.     return 0;
  1888.  
  1889.     if (vim_iswordc(c))
  1890.     return 1;
  1891.  
  1892.     /*
  1893.      * If stype is non-zero, report these as class 1.
  1894.      */
  1895.     return (stype == 0) ? 2 : 1;
  1896. }
  1897.  
  1898.  
  1899. /*
  1900.  * fwd_word(count, type, eol) - move forward one word
  1901.  *
  1902.  * Returns FAIL if the cursor was already at the end of the file.
  1903.  * If eol is TRUE, last word stops at end of line (for operators).
  1904.  */
  1905.     int
  1906. fwd_word(count, type, eol)
  1907.     long    count;
  1908.     int        type;
  1909.     int        eol;
  1910. {
  1911.     int        sclass;        /* starting class */
  1912.     int        i;
  1913.     int        last_line;
  1914.  
  1915.     stype = type;
  1916.     while (--count >= 0)
  1917.     {
  1918.     sclass = cls();
  1919.  
  1920.     /*
  1921.      * We always move at least one character, unless on the last character
  1922.      * in the buffer.
  1923.      */
  1924.     last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
  1925.     i = inc_cursor();
  1926.     if (i == -1 || (i == 1 && last_line)) /* started at last char in file */
  1927.         return FAIL;
  1928.     if (i == 1 && eol && count == 0)      /* started at last char in line */
  1929.         return OK;
  1930.  
  1931.     /*
  1932.      * Go one char past end of current word (if any)
  1933.      */
  1934.     if (sclass != 0)
  1935.         while (cls() == sclass)
  1936.         {
  1937.         i = inc_cursor();
  1938.         if (i == -1 || (i == 1 && eol && count == 0))
  1939.             return OK;
  1940.         }
  1941.  
  1942.     /*
  1943.      * go to next non-white
  1944.      */
  1945.     while (cls() == 0)
  1946.     {
  1947.         /*
  1948.          * We'll stop if we land on a blank line
  1949.          */
  1950.         if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
  1951.         break;
  1952.  
  1953.         i = inc_cursor();
  1954.         if (i == -1 || (i == 1 && eol && count == 0))
  1955.         return OK;
  1956.     }
  1957.     }
  1958.     return OK;
  1959. }
  1960.  
  1961. /*
  1962.  * bck_word() - move backward 'count' words
  1963.  *
  1964.  * If stop is TRUE and we are already on the start of a word, move one less.
  1965.  *
  1966.  * Returns FAIL if top of the file was reached.
  1967.  */
  1968.     int
  1969. bck_word(count, type, stop)
  1970.     long    count;
  1971.     int        type;
  1972.     int        stop;
  1973. {
  1974.     int        sclass;        /* starting class */
  1975.  
  1976.     stype = type;
  1977.     while (--count >= 0)
  1978.     {
  1979.     sclass = cls();
  1980.     if (dec_cursor() == -1)        /* started at start of file */
  1981.         return FAIL;
  1982.  
  1983.     if (!stop || sclass == cls() || sclass == 0)
  1984.     {
  1985.         /*
  1986.          * Skip white space before the word.
  1987.          * Stop on an empty line.
  1988.          */
  1989.         while (cls() == 0)
  1990.         {
  1991.         if (curwin->w_cursor.col == 0 &&
  1992.                          lineempty(curwin->w_cursor.lnum))
  1993.             goto finished;
  1994.  
  1995.         if (dec_cursor() == -1)        /* hit start of file, stop here */
  1996.             return OK;
  1997.         }
  1998.  
  1999.         /*
  2000.          * Move backward to start of this word.
  2001.          */
  2002.         if (skip_chars(cls(), BACKWARD))
  2003.         return OK;
  2004.     }
  2005.  
  2006.     inc_cursor();             /* overshot - forward one */
  2007. finished:
  2008.     stop = FALSE;
  2009.     }
  2010.     return OK;
  2011. }
  2012.  
  2013. /*
  2014.  * end_word() - move to the end of the word
  2015.  *
  2016.  * There is an apparent bug in the 'e' motion of the real vi. At least on the
  2017.  * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
  2018.  * motion crosses blank lines. When the real vi crosses a blank line in an
  2019.  * 'e' motion, the cursor is placed on the FIRST character of the next
  2020.  * non-blank line. The 'E' command, however, works correctly. Since this
  2021.  * appears to be a bug, I have not duplicated it here.
  2022.  *
  2023.  * Returns FAIL if end of the file was reached.
  2024.  *
  2025.  * If stop is TRUE and we are already on the end of a word, move one less.
  2026.  * If empty is TRUE stop on an empty line.
  2027.  */
  2028.     int
  2029. end_word(count, type, stop, empty)
  2030.     long    count;
  2031.     int        type;
  2032.     int        stop;
  2033.     int        empty;
  2034. {
  2035.     int        sclass;        /* starting class */
  2036.  
  2037.     stype = type;
  2038.     while (--count >= 0)
  2039.     {
  2040.     sclass = cls();
  2041.     if (inc_cursor() == -1)
  2042.         return FAIL;
  2043.  
  2044.     /*
  2045.      * If we're in the middle of a word, we just have to move to the end
  2046.      * of it.
  2047.      */
  2048.     if (cls() == sclass && sclass != 0)
  2049.     {
  2050.         /*
  2051.          * Move forward to end of the current word
  2052.          */
  2053.         if (skip_chars(sclass, FORWARD))
  2054.         return FAIL;
  2055.     }
  2056.     else if (!stop || sclass == 0)
  2057.     {
  2058.         /*
  2059.          * We were at the end of a word. Go to the end of the next word.
  2060.          * First skip white space, if 'empty' is TRUE, stop at empty line.
  2061.          */
  2062.         while (cls() == 0)
  2063.         {
  2064.         if (empty && curwin->w_cursor.col == 0 &&
  2065.                          lineempty(curwin->w_cursor.lnum))
  2066.             goto finished;
  2067.         if (inc_cursor() == -1)        /* hit end of file, stop here */
  2068.             return FAIL;
  2069.         }
  2070.  
  2071.         /*
  2072.          * Move forward to the end of this word.
  2073.          */
  2074.         if (skip_chars(cls(), FORWARD))
  2075.         return FAIL;
  2076.     }
  2077.     dec_cursor();            /* overshot - one char backward */
  2078. finished:
  2079.     stop = FALSE;            /* we move only one word less */
  2080.     }
  2081.     return OK;
  2082. }
  2083.  
  2084. /*
  2085.  * bckend_word(count, type) - move back to the end of the word
  2086.  *
  2087.  * If 'eol' is TRUE, stop at end of line.
  2088.  *
  2089.  * Returns FAIL if start of the file was reached.
  2090.  */
  2091.     int
  2092. bckend_word(count, type, eol)
  2093.     long    count;
  2094.     int        type;
  2095.     int        eol;
  2096. {
  2097.     int        sclass;        /* starting class */
  2098.     int        i;
  2099.  
  2100.     stype = type;
  2101.     while (--count >= 0)
  2102.     {
  2103.     sclass = cls();
  2104.     if ((i = dec_cursor()) == -1)
  2105.         return FAIL;
  2106.     if (eol && i == 1)
  2107.         return OK;
  2108.  
  2109.     /*
  2110.      * Move backward to before the start of this word.
  2111.      */
  2112.     if (sclass != 0)
  2113.     {
  2114.         while (cls() == sclass)
  2115.         if ((i = dec_cursor()) == -1 || (eol && i == 1))
  2116.             return OK;
  2117.     }
  2118.  
  2119.     /*
  2120.      * Move backward to end of the previous word
  2121.      */
  2122.     while (cls() == 0)
  2123.     {
  2124.         if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
  2125.         break;
  2126.         if ((i = dec_cursor()) == -1 || (eol && i == 1))
  2127.         return OK;
  2128.     }
  2129.     }
  2130.     return OK;
  2131. }
  2132.  
  2133. /*
  2134.  * Skip a row of characters of the same class.
  2135.  * Return TRUE when end-of-file reached, FALSE otherwise.
  2136.  */
  2137.     static int
  2138. skip_chars(cclass, dir)
  2139.     int        cclass;
  2140.     int        dir;
  2141. {
  2142.     while (cls() == cclass)
  2143.     if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
  2144.         return TRUE;
  2145.     return FALSE;
  2146. }
  2147.  
  2148. #ifdef TEXT_OBJECTS
  2149. /*
  2150.  * Go back to the start of the word or the start of white space
  2151.  */
  2152.     static void
  2153. back_in_line()
  2154. {
  2155.     int        sclass;            /* starting class */
  2156.  
  2157.     sclass = cls();
  2158.     for (;;)
  2159.     {
  2160.     if (curwin->w_cursor.col == 0)        /* stop at start of line */
  2161.         break;
  2162.     --curwin->w_cursor.col;
  2163.     if (cls() != sclass)            /* stop at start of word */
  2164.     {
  2165.         ++curwin->w_cursor.col;
  2166.         break;
  2167.     }
  2168.     }
  2169. }
  2170.  
  2171.     static void
  2172. find_first_blank(posp)
  2173.     FPOS    *posp;
  2174. {
  2175.     int        c;
  2176.  
  2177.     while (decl(posp) != -1)
  2178.     {
  2179.     c = gchar(posp);
  2180.     if (!vim_iswhite(c))
  2181.     {
  2182.         incl(posp);
  2183.         break;
  2184.     }
  2185.     }
  2186. }
  2187.  
  2188. /*
  2189.  * Skip count/2 sentences and count/2 separating white spaces.
  2190.  */
  2191.     static void
  2192. findsent_forward(count, at_start_sent)
  2193.     long    count;
  2194.     int        at_start_sent;    /* cursor is at start of sentence */
  2195. {
  2196.     while (count--)
  2197.     {
  2198.     findsent(FORWARD, 1L);
  2199.     if (at_start_sent)
  2200.         find_first_blank(&curwin->w_cursor);
  2201.     if (count == 0 || at_start_sent)
  2202.         decl(&curwin->w_cursor);
  2203.     at_start_sent = !at_start_sent;
  2204.     }
  2205. }
  2206.  
  2207. /*
  2208.  * Find word under cursor, cursor at end.
  2209.  * Used while an operator is pending, and in Visual mode.
  2210.  */
  2211.     int
  2212. current_word(oap, count, include, type)
  2213.     OPARG    *oap;
  2214.     long    count;
  2215.     int        include;    /* TRUE: include word and white space */
  2216.     int        type;        /* FALSE == word, TRUE == WORD */
  2217. {
  2218.     FPOS    start_pos;
  2219.     FPOS    pos;
  2220.     int        inclusive = TRUE;
  2221.  
  2222.     stype = type;
  2223.  
  2224.     /*
  2225.      * When Visual mode is not active, or when the VIsual area is only one
  2226.      * character, select the word and/or white space under the cursor.
  2227.      */
  2228.     if (!VIsual_active || equal(curwin->w_cursor, VIsual))
  2229.     {
  2230.     /*
  2231.      * Go to start of current word or white space.
  2232.      */
  2233.     back_in_line();
  2234.     start_pos = curwin->w_cursor;
  2235.  
  2236.     /*
  2237.      * If the start is on white space, and white space should be included
  2238.      * ("    word"), or start is not on white space, and white space should
  2239.      * not be included ("word"), find end of word.
  2240.      */
  2241.     if ((cls() == 0) == include)
  2242.     {
  2243.         if (end_word(1L, type, TRUE, TRUE) == FAIL)
  2244.         return FAIL;
  2245.     }
  2246.     else
  2247.     {
  2248.         /*
  2249.          * If the start is not on white space, and white space should be
  2250.          * included ("word     "), or start is on white space and white
  2251.          * space should not be included ("     "), find start of word.
  2252.          */
  2253.         if (fwd_word(1L, type, TRUE) == FAIL)
  2254.         return FAIL;
  2255.         /*
  2256.          * If end is just past a new-line, we don't want to include the
  2257.          * first character on the line
  2258.          */
  2259.         if (oneleft() == FAIL)    /* put cursor on last char of area */
  2260.         inclusive = FALSE;
  2261.         else if (include)
  2262.         {
  2263.         /*
  2264.          * If we don't include white space at the end, move the start
  2265.          * to include some white space there. This makes "daw" work
  2266.          * better on the last word in a sentence. Don't delete white
  2267.          * space at start of line (indent).
  2268.          */
  2269.         if (cls() != 0)
  2270.         {
  2271.             pos = curwin->w_cursor;    /* save cursor position */
  2272.             curwin->w_cursor = start_pos;
  2273.             if (oneleft() == OK)
  2274.             {
  2275.             back_in_line();
  2276.             if (cls() == 0 && curwin->w_cursor.col > 0)
  2277.                 start_pos = curwin->w_cursor;
  2278.             }
  2279.             curwin->w_cursor = pos;    /* put cursor back at end */
  2280.         }
  2281.         }
  2282.     }
  2283.  
  2284.     if (VIsual_active)
  2285.     {
  2286.         /* should do something when inclusive == FALSE ! */
  2287.         VIsual = start_pos;
  2288.         VIsual_mode = 'v';
  2289.         update_curbuf(NOT_VALID);        /* update the inversion */
  2290.     }
  2291.     else
  2292.     {
  2293.         oap->start = start_pos;
  2294.         oap->motion_type = MCHAR;
  2295.     }
  2296.     --count;
  2297.     }
  2298.  
  2299.     /*
  2300.      * When count is still > 0, extend with more objects.
  2301.      */
  2302.     while (count > 0)
  2303.     {
  2304.     inclusive = TRUE;
  2305.     if (VIsual_active && lt(curwin->w_cursor, VIsual))
  2306.     {
  2307.         /*
  2308.          * In Visual mode, with cursor at start: move cursor back.
  2309.          */
  2310.         if (decl(&curwin->w_cursor) == -1)
  2311.         return FAIL;
  2312.         if (include != (cls() != 0))
  2313.         {
  2314.         if (bck_word(1L, type, TRUE) == FAIL)
  2315.             return FAIL;
  2316.         }
  2317.         else
  2318.         {
  2319.         if (bckend_word(1L, type, TRUE) == FAIL)
  2320.             return FAIL;
  2321.         (void)incl(&curwin->w_cursor);
  2322.         }
  2323.     }
  2324.     else
  2325.     {
  2326.         /*
  2327.          * Move cursor forward one word and/or white area.
  2328.          */
  2329.         if (incl(&curwin->w_cursor) == -1)
  2330.         return FAIL;
  2331.         if (include != (cls() == 0))
  2332.         {
  2333.         if (fwd_word(1L, type, TRUE) == FAIL)
  2334.             return FAIL;
  2335.         /*
  2336.          * If end is just past a new-line, we don't want to include
  2337.          * the first character on the line
  2338.          */
  2339.         if (oneleft() == FAIL)    /* put cursor on last char of white */
  2340.             inclusive = FALSE;
  2341.         }
  2342.         else
  2343.         {
  2344.         if (end_word(1L, type, TRUE, TRUE) == FAIL)
  2345.             return FAIL;
  2346.         }
  2347.     }
  2348.     --count;
  2349.     }
  2350.     if (!VIsual_active)
  2351.     oap->inclusive = inclusive;
  2352.  
  2353.     return OK;
  2354. }
  2355.  
  2356. /*
  2357.  * Find sentence(s) under the cursor, cursor at end.
  2358.  * When Visual active, extend it by one or more sentences.
  2359.  */
  2360.     int
  2361. current_sent(oap, count, include)
  2362.     OPARG   *oap;
  2363.     long    count;
  2364.     int        include;
  2365. {
  2366.     FPOS    start_pos;
  2367.     FPOS    pos;
  2368.     int        start_blank;
  2369.     int        c;
  2370.     int        at_start_sent;
  2371.     long    ncount;
  2372.  
  2373.     start_pos = curwin->w_cursor;
  2374.     pos = start_pos;
  2375.     findsent(FORWARD, 1L);    /* Find start of next sentence. */
  2376.  
  2377.     /*
  2378.      * When visual area is bigger than one character: Extend it.
  2379.      */
  2380.     if (VIsual_active && !equal(start_pos, VIsual))
  2381.     {
  2382. extend:
  2383.     if (lt(start_pos, VIsual))
  2384.     {
  2385.         /*
  2386.          * Cursor at start of Visual area.
  2387.          * Find out where we are:
  2388.          * - in the white space before a sentence
  2389.          * - in a sentence or just after it
  2390.          * - at the start of a sentence
  2391.          */
  2392.         at_start_sent = TRUE;
  2393.         decl(&pos);
  2394.         while (lt(pos, curwin->w_cursor))
  2395.         {
  2396.         if (!vim_iswhite(gchar(&pos)))
  2397.         {
  2398.             at_start_sent = FALSE;
  2399.             break;
  2400.         }
  2401.         incl(&pos);
  2402.         }
  2403.         if (!at_start_sent)
  2404.         {
  2405.         findsent(BACKWARD, 1L);
  2406.         if (equal(curwin->w_cursor, start_pos))
  2407.             at_start_sent = TRUE;  /* exactly at start of sentence */
  2408.         else
  2409.             /* inside a sentence, go to its end (start of next) */
  2410.             findsent(FORWARD, 1L);
  2411.         }
  2412.         if (include)    /* "as" gets twice as much as "is" */
  2413.         count *= 2;
  2414.         while (count--)
  2415.         {
  2416.         if (at_start_sent)
  2417.             find_first_blank(&curwin->w_cursor);
  2418.         if (!at_start_sent
  2419.                 || (!include && !vim_iswhite(gchar_cursor())))
  2420.             findsent(BACKWARD, 1L);
  2421.         at_start_sent = !at_start_sent;
  2422.         }
  2423.     }
  2424.     else
  2425.     {
  2426.         /*
  2427.          * Cursor at end of Visual area.
  2428.          * Find out where we are:
  2429.          * - just before a sentence
  2430.          * - just before or in the white space before a sentence
  2431.          * - in a sentence
  2432.          */
  2433.         incl(&pos);
  2434.         at_start_sent = TRUE;
  2435.         if (!equal(pos, curwin->w_cursor))    /* not just before a sentence */
  2436.         {
  2437.         at_start_sent = FALSE;
  2438.         while (lt(pos, curwin->w_cursor))
  2439.         {
  2440.             if (!vim_iswhite(gchar(&pos)))
  2441.             {
  2442.             at_start_sent = TRUE;
  2443.             break;
  2444.             }
  2445.             incl(&pos);
  2446.         }
  2447.         if (at_start_sent)    /* in the sentence */
  2448.             findsent(BACKWARD, 1L);
  2449.         else        /* in/before white before a sentence */
  2450.             curwin->w_cursor = start_pos;
  2451.         }
  2452.  
  2453.         if (include)    /* "as" gets twice as much as "is" */
  2454.         count *= 2;
  2455.         findsent_forward(count, at_start_sent);
  2456.     }
  2457.     return OK;
  2458.     }
  2459.  
  2460.     /*
  2461.      * If cursor started on blank, check if it is just before the start of the
  2462.      * next sentence.
  2463.      */
  2464.     while (c = gchar(&pos), vim_iswhite(c))    /* vim_iswhite() is a macro */
  2465.     incl(&pos);
  2466.     if (equal(pos, curwin->w_cursor))
  2467.     {
  2468.     start_blank = TRUE;
  2469.     find_first_blank(&start_pos);    /* go back to first blank */
  2470.     }
  2471.     else
  2472.     {
  2473.     start_blank = FALSE;
  2474.     findsent(BACKWARD, 1L);
  2475.     start_pos = curwin->w_cursor;
  2476.     }
  2477.     if (include)
  2478.     ncount = count * 2;
  2479.     else
  2480.     ncount = count;
  2481.     if (!include && start_blank)
  2482.     --ncount;
  2483.     if (ncount)
  2484.     findsent_forward(ncount, TRUE);
  2485.  
  2486.     if (include)
  2487.     {
  2488.     /*
  2489.      * If the blank in front of the sentence is included, exclude the
  2490.      * blanks at the end of the sentence, go back to the first blank.
  2491.      * If there are no trailing blanks, try to include leading blanks.
  2492.      */
  2493.     if (start_blank)
  2494.         find_first_blank(&curwin->w_cursor);
  2495.     else if (c = gchar_cursor(), !vim_iswhite(c))
  2496.         find_first_blank(&start_pos);
  2497.     }
  2498.  
  2499.     if (VIsual_active)
  2500.     {
  2501.     /* avoid getting stuck with "is" on a single space before a sent. */
  2502.     if (equal(start_pos, curwin->w_cursor))
  2503.         goto extend;
  2504.     VIsual = start_pos;
  2505.     VIsual_mode = 'v';
  2506.     update_curbuf(NOT_VALID);    /* update the inversion */
  2507.     }
  2508.     else
  2509.     {
  2510.     /* include a newline after the sentence */
  2511.     incl(&curwin->w_cursor);
  2512.     oap->start = start_pos;
  2513.     oap->motion_type = MCHAR;
  2514.     oap->inclusive = FALSE;
  2515.     }
  2516.     return OK;
  2517. }
  2518.  
  2519.     int
  2520. current_block(oap, count, include, what)
  2521.     OPARG   *oap;
  2522.     long    count;
  2523.     int        include;        /* TRUE == include white space */
  2524.     int        what;        /* '(' or '{' */
  2525. {
  2526.     FPOS    old_pos;
  2527.     FPOS    *pos = NULL;
  2528.     FPOS    start_pos;
  2529.     FPOS    *end_pos;
  2530.     FPOS    old_start, old_end;
  2531.     int        other;
  2532.  
  2533.     old_pos = curwin->w_cursor;
  2534.     if (what == '{')
  2535.     other = '}';
  2536.     else
  2537.     other = ')';
  2538.  
  2539.     old_end = curwin->w_cursor;            /* remember where we started */
  2540.     old_start = old_end;
  2541.  
  2542.     /*
  2543.      * If we start on '(', '{', ')' or '}', use the whole block inclusive.
  2544.      */
  2545.     if (!VIsual_active || equal(VIsual, curwin->w_cursor))
  2546.     {
  2547.     setpcmark();
  2548.     if (what == '{')            /* ignore indent */
  2549.         while (inindent(1))
  2550.         if (inc_cursor() != 0)
  2551.             break;
  2552.     if (gchar_cursor() == what)        /* cursor on '(' or '{' */
  2553.         ++curwin->w_cursor.col;
  2554.     }
  2555.     else if (lt(VIsual, curwin->w_cursor))
  2556.     {
  2557.     old_start = VIsual;
  2558.     curwin->w_cursor = VIsual;        /* cursor at low end of Visual */
  2559.     }
  2560.     else
  2561.     old_end = VIsual;
  2562.  
  2563.     /*
  2564.      * Search backwards for unclosed '(' or '{'.
  2565.      * Put this position in start_pos.
  2566.      */
  2567.     while (count-- > 0)
  2568.     {
  2569.     if ((pos = findmatch(NULL, what)) == NULL)
  2570.         break;
  2571.     curwin->w_cursor = *pos;
  2572.     start_pos = *pos;   /* the findmatch for end_pos will overwrite *pos */
  2573.     }
  2574.  
  2575.     /*
  2576.      * Search for matching ')' or '}'.
  2577.      * Put this position in curwin->w_cursor.
  2578.      */
  2579.     if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
  2580.     {
  2581.     curwin->w_cursor = old_pos;
  2582.     return FAIL;
  2583.     }
  2584.     curwin->w_cursor = *end_pos;
  2585.  
  2586.     /*
  2587.      * Try to exclude the '(', '{', ')' and '}' when "include" is FALSE.
  2588.      * If the ending '}' is only preceded by indent, skip that indent.
  2589.      * But only if the resulting area is not smaller than what we started with.
  2590.      */
  2591.     while (!include)
  2592.     {
  2593.     incl(&start_pos);
  2594.     decl(&curwin->w_cursor);
  2595.     if (what == '{')
  2596.         while (inindent(1))
  2597.         if (decl(&curwin->w_cursor) != 0)
  2598.             break;
  2599.     /*
  2600.      * When the resulting area is not bigger than what we started with,
  2601.      * extend it to the next block, and then exclude again.
  2602.      */
  2603.     if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor))
  2604.     {
  2605.         curwin->w_cursor = old_start;
  2606.         decl(&curwin->w_cursor);
  2607.         if ((pos = findmatch(NULL, what)) == NULL)
  2608.         {
  2609.         curwin->w_cursor = old_pos;
  2610.         return FAIL;
  2611.         }
  2612.         start_pos = *pos;
  2613.         curwin->w_cursor = *pos;
  2614.         if ((end_pos = findmatch(NULL, other)) == NULL)
  2615.         {
  2616.         curwin->w_cursor = old_pos;
  2617.         return FAIL;
  2618.         }
  2619.         curwin->w_cursor = *end_pos;
  2620.     }
  2621.     else
  2622.         break;
  2623.     }
  2624.  
  2625.     if (VIsual_active)
  2626.     {
  2627.     VIsual = start_pos;
  2628.     VIsual_mode = 'v';
  2629.     update_curbuf(NOT_VALID);    /* update the inversion */
  2630.     showmode();
  2631.     }
  2632.     else
  2633.     {
  2634.     oap->start = start_pos;
  2635.     oap->motion_type = MCHAR;
  2636.     oap->inclusive = TRUE;
  2637.     }
  2638.  
  2639.     return OK;
  2640. }
  2641.  
  2642.     int
  2643. current_par(oap, count, include, type)
  2644.     OPARG   *oap;
  2645.     long    count;
  2646.     int        include;        /* TRUE == include white space */
  2647.     int        type;        /* 'p' for paragraph, 'S' for section */
  2648. {
  2649.     linenr_t    start_lnum;
  2650.     linenr_t    end_lnum;
  2651.     int        white_in_front;
  2652.     int        dir;
  2653.     int        start_is_white;
  2654.     int        prev_start_is_white;
  2655.     int        retval = OK;
  2656.     int        do_white = FALSE;
  2657.     int        t;
  2658.     int        i;
  2659.  
  2660.     if (type == 'S')        /* not implemented yet */
  2661.     return FAIL;
  2662.  
  2663.     start_lnum = curwin->w_cursor.lnum;
  2664.  
  2665.     /*
  2666.      * When visual area is more than one line: extend it.
  2667.      */
  2668.     if (VIsual_active && start_lnum != VIsual.lnum)
  2669.     {
  2670. extend:
  2671.     if (start_lnum < VIsual.lnum)
  2672.         dir = BACKWARD;
  2673.     else
  2674.         dir = FORWARD;
  2675.     for (i = count; --i >= 0; )
  2676.     {
  2677.         if (start_lnum ==
  2678.                (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
  2679.         {
  2680.         retval = FAIL;
  2681.         break;
  2682.         }
  2683.  
  2684.         prev_start_is_white = -1;
  2685.         for (t = 0; t < 2; ++t)
  2686.         {
  2687.         start_lnum += dir;
  2688.         start_is_white = linewhite(start_lnum);
  2689.         if (prev_start_is_white == start_is_white)
  2690.         {
  2691.             start_lnum -= dir;
  2692.             break;
  2693.         }
  2694.         for (;;)
  2695.         {
  2696.             if (start_lnum == (dir == BACKWARD
  2697.                         ? 1 : curbuf->b_ml.ml_line_count))
  2698.             break;
  2699.             if (start_is_white != linewhite(start_lnum + dir)
  2700.                 || (!start_is_white
  2701.                     && startPS(start_lnum + (dir > 0
  2702.                                  ? 1 : 0), 0, 0)))
  2703.             break;
  2704.             start_lnum += dir;
  2705.         }
  2706.         if (!include)
  2707.             break;
  2708.         if (start_lnum == (dir == BACKWARD
  2709.                         ? 1 : curbuf->b_ml.ml_line_count))
  2710.             break;
  2711.         prev_start_is_white = start_is_white;
  2712.         }
  2713.     }
  2714.     curwin->w_cursor.lnum = start_lnum;
  2715.     curwin->w_cursor.col = 0;
  2716.     return retval;
  2717.     }
  2718.  
  2719.     /*
  2720.      * First move back to the start_lnum of the paragraph or white lines
  2721.      */
  2722.     white_in_front = linewhite(start_lnum);
  2723.     while (start_lnum > 1)
  2724.     {
  2725.     if (white_in_front)        /* stop at first white line */
  2726.     {
  2727.         if (!linewhite(start_lnum - 1))
  2728.         break;
  2729.     }
  2730.     else        /* stop at first non-white line of start of paragraph */
  2731.     {
  2732.         if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
  2733.         break;
  2734.     }
  2735.     --start_lnum;
  2736.     }
  2737.  
  2738.     /*
  2739.      * Move past the end of any white lines.
  2740.      */
  2741.     end_lnum = start_lnum;
  2742.     while (linewhite(end_lnum) && end_lnum < curbuf->b_ml.ml_line_count)
  2743.         ++end_lnum;
  2744.  
  2745.     --end_lnum;
  2746.     i = count;
  2747.     if (!include && white_in_front)
  2748.     --i;
  2749.     while (i--)
  2750.     {
  2751.     if (end_lnum == curbuf->b_ml.ml_line_count)
  2752.         return FAIL;
  2753.  
  2754.     if (!include)
  2755.         do_white = linewhite(end_lnum + 1);
  2756.  
  2757.     if (include || !do_white)
  2758.     {
  2759.         ++end_lnum;
  2760.         /*
  2761.          * skip to end of paragraph
  2762.          */
  2763.         while (end_lnum < curbuf->b_ml.ml_line_count
  2764.             && !linewhite(end_lnum + 1)
  2765.             && !startPS(end_lnum + 1, 0, 0))
  2766.         ++end_lnum;
  2767.     }
  2768.  
  2769.     if (i == 0 && white_in_front)
  2770.         break;
  2771.  
  2772.     /*
  2773.      * skip to end of white lines after paragraph
  2774.      */
  2775.     if (include || do_white)
  2776.         while (end_lnum < curbuf->b_ml.ml_line_count
  2777.                            && linewhite(end_lnum + 1))
  2778.         ++end_lnum;
  2779.     }
  2780.  
  2781.     /*
  2782.      * If there are no empty lines at the end, try to find some empty lines at
  2783.      * the start (unless that has been done already).
  2784.      */
  2785.     if (!white_in_front && !linewhite(end_lnum) && include)
  2786.     while (start_lnum > 1 && linewhite(start_lnum - 1))
  2787.         --start_lnum;
  2788.  
  2789.     if (VIsual_active)
  2790.     {
  2791.     /* Problem: when doing "Vipipip" nothing happens in a single white
  2792.      * line, we get stuck there.  Trap this here. */
  2793.     if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
  2794.         goto extend;
  2795.     VIsual.lnum = start_lnum;
  2796.     VIsual_mode = 'V';
  2797.     update_curbuf(NOT_VALID);    /* update the inversion */
  2798.     showmode();
  2799.     }
  2800.     else
  2801.     {
  2802.     oap->start.lnum = start_lnum;
  2803.     oap->motion_type = MLINE;
  2804.     }
  2805.     curwin->w_cursor.lnum = end_lnum;
  2806.     curwin->w_cursor.col = 0;
  2807.  
  2808.     return OK;
  2809. }
  2810. #endif
  2811.  
  2812. /*
  2813.  * linewhite -- return TRUE if line 'lnum' is empty or has white chars only.
  2814.  */
  2815.     int
  2816. linewhite(lnum)
  2817.     linenr_t    lnum;
  2818. {
  2819.     char_u  *p;
  2820.  
  2821.     p = skipwhite(ml_get(lnum));
  2822.     return (*p == NUL);
  2823. }
  2824.  
  2825. #ifdef FIND_IN_PATH
  2826. /*
  2827.  * Find identifiers or defines in included files.
  2828.  * if p_ic && (continue_status & CONT_SOL) then ptr must be in lowercase.
  2829.  */
  2830.     void
  2831. find_pattern_in_path(ptr, dir, len, whole, skip_comments,
  2832.                     type, count, action, start_lnum, end_lnum)
  2833.     char_u  *ptr;        /* pointer to search pattern */
  2834.     int        dir;        /* direction of expantion */
  2835.     int        len;        /* length of search pattern */
  2836.     int        whole;        /* match whole words only */
  2837.     int        skip_comments;  /* don't match inside comments */
  2838.     int        type;        /* Type of search; are we looking for a type?  a
  2839.                 macro? */
  2840.     long    count;
  2841.     int        action;        /* What to do when we find it */
  2842.     linenr_t    start_lnum; /* first line to start searching */
  2843.     linenr_t    end_lnum;   /* last line for searching */
  2844. {
  2845.     SearchedFile *files;        /* Stack of included files */
  2846.     SearchedFile *bigger;        /* When we need more space */
  2847.     int        max_path_depth = 50;
  2848.     long    match_count = 1;
  2849.  
  2850.     char_u    *pat;
  2851.     int        pat_reg_ic = FALSE;
  2852.     char_u    *new_fname;
  2853.     char_u    *curr_fname = curbuf->b_fname;
  2854.     char_u    *prev_fname = NULL;
  2855.     linenr_t    lnum;
  2856.     int        depth;
  2857.     int        depth_displayed;    /* For type==CHECK_PATH */
  2858.     int        old_files;
  2859.     int        already_searched;
  2860.     char_u    *file_line;
  2861.     char_u    *line;
  2862.     char_u    *p;
  2863.     char_u    save_char;
  2864.     int        define_matched;
  2865.     vim_regexp    *prog = NULL;
  2866.     vim_regexp    *include_prog = NULL;
  2867.     vim_regexp    *define_prog = NULL;
  2868.     int        matched = FALSE;
  2869.     int        did_show = FALSE;
  2870.     int        found = FALSE;
  2871.     int        i;
  2872.     char_u    *already = NULL;
  2873.     char_u    *startp = NULL;
  2874.  
  2875.     file_line = alloc(LSIZE);
  2876.     if (file_line == NULL)
  2877.     return;
  2878.  
  2879.     if (type != CHECK_PATH && type != FIND_DEFINE
  2880. #ifdef INSERT_EXPAND
  2881.     /* when CONT_SOL is set compare "ptr" with the beginning of the line
  2882.      * is faster than quote_meta/regcomp/regexep "ptr" -- Acevedo */
  2883.         && !(continue_status & CONT_SOL)
  2884. #endif
  2885.        )
  2886.     {
  2887.     pat = alloc(len + 5);
  2888.     if (pat == NULL)
  2889.         goto fpip_end;
  2890.     sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
  2891.     set_reg_ic(pat);    /* set reg_ic according to p_ic, p_scs and pat */
  2892.     pat_reg_ic = reg_ic;
  2893.     prog = vim_regcomp(pat, (int)p_magic);
  2894.     vim_free(pat);
  2895.     if (prog == NULL)
  2896.         goto fpip_end;
  2897.     }
  2898.     if (*p_inc != NUL)
  2899.     {
  2900.     include_prog = vim_regcomp(p_inc, (int)p_magic);
  2901.     if (include_prog == NULL)
  2902.         goto fpip_end;
  2903.     }
  2904.     if (type == FIND_DEFINE && *p_def != NUL)
  2905.     {
  2906.     define_prog = vim_regcomp(p_def, (int)p_magic);
  2907.     if (define_prog == NULL)
  2908.         goto fpip_end;
  2909.     }
  2910.     files = (SearchedFile *)lalloc((long_u)
  2911.                    (max_path_depth * sizeof(SearchedFile)), TRUE);
  2912.     if (files == NULL)
  2913.     goto fpip_end;
  2914.     for (i = 0; i < max_path_depth; i++)
  2915.     {
  2916.     files[i].fp = NULL;
  2917.     files[i].name = NULL;
  2918.     files[i].lnum = 0;
  2919.     files[i].matched = FALSE;
  2920.     }
  2921.     old_files = max_path_depth;
  2922.     depth = depth_displayed = -1;
  2923.  
  2924.     lnum = start_lnum;
  2925.     if (end_lnum > curbuf->b_ml.ml_line_count)
  2926.     end_lnum = curbuf->b_ml.ml_line_count;
  2927.     if (lnum > end_lnum)        /* do at least one line */
  2928.     lnum = end_lnum;
  2929.     line = ml_get(lnum);
  2930.  
  2931.     for (;;)
  2932.     {
  2933.     reg_ic = FALSE;    /* don't ignore case in include pattern */
  2934.     if (include_prog != NULL && vim_regexec(include_prog, line, TRUE))
  2935.     {
  2936.         new_fname = get_file_name_in_path(include_prog->endp[0] + 1,
  2937.                                 0, FNAME_EXP, 1L);
  2938.         already_searched = FALSE;
  2939.         if (new_fname != NULL)
  2940.         {
  2941.         /* Check whether we have already searched in this file */
  2942.         for (i = 0;; i++)
  2943.         {
  2944.             if (i == depth + 1)
  2945.             i = old_files;
  2946.             if (i == max_path_depth)
  2947.             break;
  2948.             if (STRCMP(new_fname, files[i].name) == 0)
  2949.             {
  2950.             if (type != CHECK_PATH &&
  2951.                 action == ACTION_SHOW_ALL && files[i].matched)
  2952.             {
  2953.                 msg_putchar('\n');        /* cursor below last one */
  2954.                 if (!got_int)        /* don't display if 'q'
  2955.                                typed at "--more--"
  2956.                                mesage */
  2957.                 {
  2958.                 msg_home_replace_hl(new_fname);
  2959.                 MSG_PUTS(" (includes previously listed match)");
  2960.                 prev_fname = NULL;
  2961.                 }
  2962.             }
  2963.             vim_free(new_fname);
  2964.             new_fname = NULL;
  2965.             already_searched = TRUE;
  2966.             break;
  2967.             }
  2968.         }
  2969.         }
  2970.  
  2971.         if (type == CHECK_PATH && (action == ACTION_SHOW_ALL ||
  2972.                     (new_fname == NULL && !already_searched)))
  2973.         {
  2974.         if (did_show)
  2975.             msg_putchar('\n');        /* cursor below last one */
  2976.         else
  2977.         {
  2978.             gotocmdline(TRUE);        /* cursor at status line */
  2979.             MSG_PUTS_TITLE("--- Included files ");
  2980.             if (action != ACTION_SHOW_ALL)
  2981.             MSG_PUTS_TITLE("not found ");
  2982.             MSG_PUTS_TITLE("in path ---\n");
  2983.         }
  2984.         did_show = TRUE;
  2985.         while (depth_displayed < depth && !got_int)
  2986.         {
  2987.             ++depth_displayed;
  2988.             for (i = 0; i < depth_displayed; i++)
  2989.             MSG_PUTS("  ");
  2990.             msg_home_replace(files[depth_displayed].name);
  2991.             MSG_PUTS(" -->\n");
  2992.         }
  2993.         if (!got_int)            /* don't display if 'q' typed
  2994.                            for "--more--" message */
  2995.         {
  2996.             for (i = 0; i <= depth_displayed; i++)
  2997.             MSG_PUTS("  ");
  2998.             /*
  2999.              * Isolate the file name.
  3000.              * Include the surrounding "" or <> if present.
  3001.              */
  3002.             for (p = include_prog->endp[0] + 1; !vim_isfilec(*p); p++)
  3003.             ;
  3004.             for (i = 0; vim_isfilec(p[i]); i++)
  3005.             ;
  3006.             if (p[-1] == '"' || p[-1] == '<')
  3007.             {
  3008.             --p;
  3009.             ++i;
  3010.             }
  3011.             if (p[i] == '"' || p[i] == '>')
  3012.             ++i;
  3013.             save_char = p[i];
  3014.             p[i] = NUL;
  3015.                 /* Same highlighting as for directories */
  3016.             msg_outtrans_attr(p, highlight_attr[HLF_D]);
  3017.             p[i] = save_char;
  3018.             if (new_fname == NULL && action == ACTION_SHOW_ALL)
  3019.             {
  3020.             if (already_searched)
  3021.                 MSG_PUTS("  (Already listed)");
  3022.             else
  3023.                 MSG_PUTS("  NOT FOUND");
  3024.             }
  3025.         }
  3026.         out_flush();        /* output each line directly */
  3027.         }
  3028.  
  3029.         if (new_fname != NULL)
  3030.         {
  3031.         /* Push the new file onto the file stack */
  3032.         if (depth + 1 == old_files)
  3033.         {
  3034.             bigger = (SearchedFile *)lalloc((long_u)(
  3035.                 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
  3036.             if (bigger != NULL)
  3037.             {
  3038.             for (i = 0; i <= depth; i++)
  3039.                 bigger[i] = files[i];
  3040.             for (i = depth + 1; i < old_files + max_path_depth; i++)
  3041.             {
  3042.                 bigger[i].fp = NULL;
  3043.                 bigger[i].name = NULL;
  3044.                 bigger[i].lnum = 0;
  3045.                 bigger[i].matched = FALSE;
  3046.             }
  3047.             for (i = old_files; i < max_path_depth; i++)
  3048.                 bigger[i + max_path_depth] = files[i];
  3049.             old_files += max_path_depth;
  3050.             max_path_depth *= 2;
  3051.             vim_free(files);
  3052.             files = bigger;
  3053.             }
  3054.         }
  3055.         if ((files[depth + 1].fp = fopen((char *)new_fname, "r"))
  3056.                                     == NULL)
  3057.             vim_free(new_fname);
  3058.         else
  3059.         {
  3060.             if (++depth == old_files)
  3061.             {
  3062.             /*
  3063.              * lalloc() for 'bigger' must have failed above.  We
  3064.              * will forget one of our already visited files now.
  3065.              */
  3066.             vim_free(files[old_files].name);
  3067.             ++old_files;
  3068.             }
  3069.             files[depth].name = curr_fname = new_fname;
  3070.             files[depth].lnum = 0;
  3071.             files[depth].matched = FALSE;
  3072.         }
  3073.         }
  3074.     }
  3075.     else
  3076.     {
  3077.         /*
  3078.          * Check if the line is a define (type == FIND_DEFINE)
  3079.          */
  3080.         p = line;
  3081. search_line:
  3082.         define_matched = FALSE;
  3083.         reg_ic = FALSE;    /* don't ignore case in define patterns */
  3084.         if (define_prog != NULL && vim_regexec(define_prog, line, TRUE))
  3085.         {
  3086.         /*
  3087.          * Pattern must be first identifier after 'define', so skip
  3088.          * to that position before checking for match of pattern.  Also
  3089.          * don't let it match beyond the end of this identifier.
  3090.          */
  3091.         p = define_prog->endp[0] + 1;
  3092.         while (*p && !vim_isIDc(*p))
  3093.             p++;
  3094.         define_matched = TRUE;
  3095.         }
  3096.  
  3097.         /*
  3098.          * Look for a match.  Don't do this if we are looking for a
  3099.          * define and this line didn't match define_prog above.
  3100.          */
  3101.         if (define_prog == NULL || define_matched)
  3102.         {
  3103.         reg_ic = pat_reg_ic;
  3104.         if (define_matched
  3105. #ifdef INSERT_EXPAND
  3106.             || (continue_status & CONT_SOL)
  3107. #endif
  3108.             )
  3109.         {
  3110.             /* compare the first "len" chars from "ptr" */
  3111.             startp = skipwhite(p);
  3112.             if (p_ic)
  3113.             matched = !STRNICMP(startp, ptr, len);
  3114.             else
  3115.             matched = !STRNCMP(startp, ptr, len);
  3116.             if (matched && define_matched && whole
  3117.                             && vim_isIDc(startp[len]))
  3118.             matched = FALSE;
  3119.         }
  3120.         else if (prog && vim_regexec(prog, p, p == line))
  3121.         {
  3122.             matched = TRUE;
  3123.             startp = prog->startp[0];
  3124.             /*
  3125.              * Check if the line is not a comment line (unless we are
  3126.              * looking for a define).  A line starting with "# define"
  3127.              * is not considered to be a comment line.
  3128.              */
  3129.             if (!define_matched && skip_comments)
  3130.             {
  3131.             fo_do_comments = TRUE;
  3132.             if ((*line != '#' ||
  3133.                 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
  3134.                 && get_leader_len(line, NULL))
  3135.                 matched = FALSE;
  3136.  
  3137.             /*
  3138.              * Also check for a "/ *" or "/ /" before the match.
  3139.              * Skips lines like "int idx;  / * normal index * /"
  3140.              * when looking for "normal".
  3141.              */
  3142.             else
  3143.                 for (p = line; *p && p < startp; ++p)
  3144.                 if (p[0] == '/' && (p[1] == '*' || p[1] == '/'))
  3145.                 {
  3146.                     matched = FALSE;
  3147.                     break;
  3148.                 }
  3149.             fo_do_comments = FALSE;
  3150.             }
  3151.         }
  3152.         }
  3153.     }
  3154.     if (matched)
  3155.     {
  3156. #ifdef INSERT_EXPAND
  3157.         if (action == ACTION_EXPAND)
  3158.         {
  3159.         int    reuse = 0;
  3160.         int    add_r;
  3161.         char_u    *aux;
  3162.  
  3163.         if (depth == -1 && lnum == curwin->w_cursor.lnum)
  3164.             break;
  3165.         found = TRUE;
  3166.         aux = p = startp;
  3167.         if (continue_status & CONT_ADDING)
  3168.         {
  3169.             p += completion_length;
  3170.             if (vim_iswordc(*p))
  3171.             goto exit_matched;
  3172.             while (*p && *p != '\n' && !vim_iswordc(*p++))
  3173.             ;
  3174.         }
  3175.         while (vim_iswordc(*p))
  3176.             ++p;
  3177.         i = p - aux;
  3178.  
  3179.         if ((continue_status & CONT_ADDING) && i == completion_length)
  3180.         {
  3181.             /* get the next line */
  3182.             /* IOSIZE > completion_length, so the STRNCPY works */
  3183.             STRNCPY(IObuff, aux, i);
  3184.             if (!(     depth < 0
  3185.                 && lnum < end_lnum
  3186.                 && (line = ml_get(++lnum)) != NULL)
  3187.             && !(    depth >= 0
  3188.                 && !vim_fgets(line = file_line,
  3189.                              LSIZE, files[depth].fp)))
  3190.             goto exit_matched;
  3191.  
  3192.             /* we read a line, set "already" to check this "line" later
  3193.              * if depth >= 0 we'll increase files[depth].lnum far
  3194.              * bellow  -- Acevedo */
  3195.             already = aux = p = skipwhite(line);
  3196.             while (*p && *p != '\n' && !vim_iswordc(*p++))
  3197.             ;
  3198.             while (vim_iswordc(*p))
  3199.             p++;
  3200.             if (p > aux)
  3201.             {
  3202.             if (*aux != ')' && IObuff[i-1] != TAB)
  3203.             {
  3204.                 if (IObuff[i-1] != ' ')
  3205.                 IObuff[i++] = ' ';
  3206.                 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
  3207.                 if (p_js
  3208.                 && (IObuff[i-2] == '.'
  3209.                     || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
  3210.                     && (IObuff[i-2] == '?'
  3211.                         || IObuff[i-2] == '!'))))
  3212.                 IObuff[i++] = ' ';
  3213.             }
  3214.             /* copy as much as posible of the new word */
  3215.             if (p - aux >= IOSIZE - i)
  3216.                 p = aux + IOSIZE - i - 1;
  3217.             STRNCPY(IObuff + i, aux, p - aux);
  3218.             i += p - aux;
  3219.             reuse |= CONT_S_IPOS;
  3220.             }
  3221.             IObuff[i] = NUL;
  3222.             aux = IObuff;
  3223.  
  3224.             if (i == completion_length)
  3225.             goto exit_matched;
  3226.         }
  3227.  
  3228.         add_r = add_completion_and_infercase(aux, i,
  3229.             curr_fname == curbuf->b_fname ? NULL : curr_fname,
  3230.             dir, reuse);
  3231.         if (add_r == OK)
  3232.             /* if dir was BACKWARD then honor it just once */
  3233.             dir = FORWARD;
  3234.         else if (add_r == RET_ERROR)
  3235.             break;
  3236.         }
  3237.         else
  3238. #endif
  3239.          if (action == ACTION_SHOW_ALL)
  3240.         {
  3241.         found = TRUE;
  3242.         if (!did_show)
  3243.             gotocmdline(TRUE);        /* cursor at status line */
  3244.         if (curr_fname != prev_fname)
  3245.         {
  3246.             if (did_show)
  3247.             msg_putchar('\n');    /* cursor below last one */
  3248.             if (!got_int)        /* don't display if 'q' typed
  3249.                             at "--more--" mesage */
  3250.             msg_home_replace_hl(curr_fname);
  3251.             prev_fname = curr_fname;
  3252.         }
  3253.         did_show = TRUE;
  3254.         if (!got_int)
  3255.             show_pat_in_path(line, type, TRUE, action,
  3256.                 (depth == -1) ? NULL : files[depth].fp,
  3257.                 (depth == -1) ? &lnum : &files[depth].lnum,
  3258.                 match_count++);
  3259.  
  3260.         /* Set matched flag for this file and all the ones that
  3261.          * include it */
  3262.         for (i = 0; i <= depth; ++i)
  3263.             files[i].matched = TRUE;
  3264.         }
  3265.         else if (--count <= 0)
  3266.         {
  3267.         found = TRUE;
  3268.         if (depth == -1 && lnum == curwin->w_cursor.lnum)
  3269.             EMSG("Match is on current line");
  3270.         else if (action == ACTION_SHOW)
  3271.         {
  3272.             show_pat_in_path(line, type, did_show, action,
  3273.             (depth == -1) ? NULL : files[depth].fp,
  3274.             (depth == -1) ? &lnum : &files[depth].lnum, 1L);
  3275.             did_show = TRUE;
  3276.         }
  3277.         else
  3278.         {
  3279.             if (action == ACTION_SPLIT)
  3280.             {
  3281.             if (win_split(0, FALSE, FALSE) == FAIL)
  3282.                 break;
  3283.             }
  3284.             if (depth == -1)
  3285.             {
  3286.             setpcmark();
  3287.             curwin->w_cursor.lnum = lnum;
  3288.             }
  3289.             else
  3290.             if (getfile(0, files[depth].name, NULL, TRUE,
  3291.                         files[depth].lnum, FALSE) > 0)
  3292.                 break;    /* failed to jump to file */
  3293.         }
  3294.         if (action != ACTION_SHOW)
  3295.         {
  3296.             curwin->w_cursor.col = startp - line;
  3297.             curwin->w_set_curswant = TRUE;
  3298.         }
  3299.         break;
  3300.         }
  3301. #ifdef INSERT_EXPAND
  3302. exit_matched:
  3303. #endif
  3304.         matched = FALSE;
  3305.         /* look for other matches in the rest of the line if we
  3306.          * are not at the end of it already */
  3307.         if (define_prog == NULL
  3308. #ifdef INSERT_EXPAND
  3309.             && action == ACTION_EXPAND
  3310.             && !(continue_status & CONT_SOL)
  3311. #endif
  3312.             && *(p = startp + 1))
  3313.         goto search_line;
  3314.     }
  3315.     line_breakcheck();
  3316.     if (got_int)
  3317.         break;
  3318.     while (depth >= 0 && !already &&
  3319.            vim_fgets(line = file_line, LSIZE, files[depth].fp))
  3320.     {
  3321.         fclose(files[depth].fp);
  3322.         --old_files;
  3323.         files[old_files].name = files[depth].name;
  3324.         files[old_files].matched = files[depth].matched;
  3325.         --depth;
  3326.         curr_fname = (depth == -1) ? curbuf->b_fname
  3327.                        : files[depth].name;
  3328.         if (depth < depth_displayed)
  3329.         depth_displayed = depth;
  3330.     }
  3331.     if (depth >= 0)        /* we could read the line */
  3332.         files[depth].lnum++;
  3333.     else if (!already)
  3334.     {
  3335.         if (++lnum > end_lnum)
  3336.         break;
  3337.         line = ml_get(lnum);
  3338.     }
  3339.     already = NULL;
  3340.     }
  3341.     for (i = 0; i <= depth; i++)
  3342.     {
  3343.     fclose(files[i].fp);
  3344.     vim_free(files[i].name);
  3345.     }
  3346.     for (i = old_files; i < max_path_depth; i++)
  3347.     vim_free(files[i].name);
  3348.     vim_free(files);
  3349.  
  3350.     if (type == CHECK_PATH)
  3351.     {
  3352.     if (!did_show)
  3353.     {
  3354.         if (action != ACTION_SHOW_ALL)
  3355.         MSG("All included files were found");
  3356.         else
  3357.         MSG("No included files");
  3358.     }
  3359.     }
  3360.     else if (!found
  3361. #ifdef INSERT_EXPAND
  3362.             && action != ACTION_EXPAND
  3363. #endif
  3364.                         )
  3365.     {
  3366.     if (got_int)
  3367.         emsg(e_interr);
  3368.     else if (type == FIND_DEFINE)
  3369.         EMSG("Couldn't find definition");
  3370.     else
  3371.         EMSG("Couldn't find pattern");
  3372.     }
  3373.     if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
  3374.     msg_end();
  3375.  
  3376. fpip_end:
  3377.     vim_free(file_line);
  3378. #ifdef INSERT_EXPAND
  3379.     if (!(continue_status & CONT_SOL))
  3380. #endif
  3381.     vim_free(prog);
  3382.     vim_free(include_prog);
  3383.     vim_free(define_prog);
  3384. }
  3385.  
  3386.     static void
  3387. show_pat_in_path(line, type, did_show, action, fp, lnum, count)
  3388.     char_u  *line;
  3389.     int        type;
  3390.     int        did_show;
  3391.     int        action;
  3392.     FILE    *fp;
  3393.     linenr_t *lnum;
  3394.     long    count;
  3395. {
  3396.     char_u  *p;
  3397.  
  3398.     if (did_show)
  3399.     msg_putchar('\n');    /* cursor below last one */
  3400.     else
  3401.     gotocmdline(TRUE);    /* cursor at status line */
  3402.     if (got_int)        /* 'q' typed at "--more--" message */
  3403.     return;
  3404.     for (;;)
  3405.     {
  3406.     p = line + STRLEN(line) - 1;
  3407.     if (fp != NULL)
  3408.     {
  3409.         /* We used fgets(), so get rid of newline at end */
  3410.         if (p >= line && *p == '\n')
  3411.         --p;
  3412.         if (p >= line && *p == '\r')
  3413.         --p;
  3414.         *(p + 1) = NUL;
  3415.     }
  3416.     if (action == ACTION_SHOW_ALL)
  3417.     {
  3418.         sprintf((char *)IObuff, "%3ld: ", count);    /* show match nr */
  3419.         msg_puts(IObuff);
  3420.         sprintf((char *)IObuff, "%4ld", *lnum);    /* show line nr */
  3421.                         /* Highlight line numbers */
  3422.         msg_puts_attr(IObuff, highlight_attr[HLF_N]);
  3423.         MSG_PUTS(" ");
  3424.     }
  3425.     msg_prt_line(line);
  3426.     out_flush();            /* show one line at a time */
  3427.  
  3428.     /* Definition continues until line that doesn't end with '\' */
  3429.     if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
  3430.         break;
  3431.  
  3432.     if (fp != NULL)
  3433.     {
  3434.         if (vim_fgets(line, LSIZE, fp)) /* end of file */
  3435.         break;
  3436.         ++*lnum;
  3437.     }
  3438.     else
  3439.     {
  3440.         if (++*lnum > curbuf->b_ml.ml_line_count)
  3441.         break;
  3442.         line = ml_get(*lnum);
  3443.     }
  3444.     msg_putchar('\n');
  3445.     }
  3446. }
  3447. #endif
  3448.  
  3449. #ifdef VIMINFO
  3450.     int
  3451. read_viminfo_search_pattern(line, fp, force)
  3452.     char_u  *line;
  3453.     FILE    *fp;
  3454.     int        force;
  3455. {
  3456.     char_u  *lp;
  3457.     char_u  **pattern;
  3458.     int        idx;
  3459.  
  3460.     lp = line;
  3461.     if (lp[0] == '~')        /* use this pattern for last-used pattern */
  3462.     lp++;
  3463.     if (lp[0] == '/')
  3464.     idx = RE_SEARCH;
  3465.     else
  3466.     idx = RE_SUBST;
  3467.     pattern = &spats[idx].pat;
  3468.     if (force || *pattern == NULL)
  3469.     {
  3470.     vim_free(*pattern);
  3471.     viminfo_readstring(lp);
  3472.     *pattern = vim_strsave(lp + 1);
  3473.     if (line[0] == '~')
  3474.     {
  3475.         last_idx = idx;
  3476. #ifdef EXTRA_SEARCH
  3477.         /* If 'hlsearch' set and search pat changed: need redraw. */
  3478.         if (p_hls)
  3479.         redraw_all_later(NOT_VALID);
  3480. #endif
  3481.     }
  3482.     }
  3483.     return vim_fgets(line, LSIZE, fp);
  3484. }
  3485.  
  3486.     void
  3487. write_viminfo_search_pattern(fp)
  3488.     FILE    *fp;
  3489. {
  3490.     if (get_viminfo_parameter('/') != 0)
  3491.     {
  3492.     wvsp_one(fp, RE_SEARCH, "", "/");
  3493.     wvsp_one(fp, RE_SUBST, "Substitute ", "&");
  3494.     }
  3495. }
  3496.  
  3497.     static void
  3498. wvsp_one(fp, idx, s, sc)
  3499.     FILE    *fp;
  3500.     int        idx;
  3501.     char    *s;
  3502.     char    *sc;
  3503. {
  3504.     if (spats[idx].pat != NULL)
  3505.     {
  3506.     fprintf(fp, "\n# Last %sSearch Pattern:\n", s);
  3507.     if (last_idx == idx)
  3508.         fprintf(fp, "~");
  3509.     fprintf(fp, sc);
  3510.     viminfo_writestring(fp, spats[idx].pat);
  3511.     }
  3512. }
  3513. #endif /* VIMINFO */
  3514.